·  C – Create – Library – DLL Run-time

 

- Info:

  - This tutorial shows how to create "C Run-time DLL" using different compilers and tools.

  - More about DLLs and Load Time DLLs can be found at DLL and  Run Time DLL.

  - This info is based on http://www.coopknow.com/papers.asp?paper=4.

 

- MinGW:

  - Create C:\Temp\Test.h, Test.c and Hello.c

  - Start MSDOS

  - gcc -shared Test.c -o Test.dll        (creates Test.dll)

  - gcc Hello.c -o Hello.exe             (creates Hello.exe)

  - Hello.exe

 

- Microsoft Compiler:

  - Create C:\Temp\Test.c

  - Start MSDOS

  - cd C:\Temp

  - cl /c Test.c                         (creates Test.obj)

  - link -dll Test.obj                   (creates Test.dll, Test.lib and Test.exp)

  - cl Hello.c                           (creates Hello.exe)

  - Hello.exe

 

- Microsoft Visual Studio 2008:

  - Start MVS 2008 – File – New – Project ... – Visual C++ – General – Empty Project – Name: MyProject – OK

  - RC on MyProject – Properties... – Configuration Properties – General – Project Defaults

    Configuration Type: Dynamic Library (.dll) – OK

  - RC on Source Files – Add – New Item... – Code – C++ File (.cpp) – Name: Test.c – Add – Paste Test.c

  - RC on Test.c – Properties – Configuration Properties – C/C++ – Advanced – Compile As: Compile as C Code (/TC)

  - Start Without Debugging Ctrl+F5

 

    Test.h

    __declspec(dllexport) void hello(void);

   

    Test.c

    #include <stdio.h> 

    #include "Test.h"

   

    void hello(void) { printf("Hello from DLL!\n"); }

   

    Hello.c

    #include <stdio.h> 

    #include <windows.h>

   

    int main () {

      

      {

        //WAIT FOR USER INPUT.---------------------------------------------------------------------

        int i;

        printf("Enter number:");

        scanf ("%i", &i);

     

      

      {

        //LOAD DLL INTO MEMORY.--------------------------------------------------------------------

        HANDLE  hdll  = LoadLibrary("Test.dll");               //LoadLibrary. FreeLibrary

     

        //CALL FUNCTION FROM DLL.------------------------------------------------------------------

        typedef void (*pfunc)();                               //Typedef the hello function.  

        pfunc   hello = (pfunc) GetProcAddress(hdll, "hello"); //A pointer to a function/procedure.

                hello();

      }

   

      return 0;

      

    }