·  MinGW – Create Application – C – DLL – Run-time dynamic linking


- Info:

  - This tutorial shows how to use MinGW and C to create DLL which is loaded into RAM by executable during run-time.

  - If you delete message.dll you will still be able to start hello.exe.

    But after you enter some number hello.exe will try to load message.dll and if it is not there exception will occur.


- Procedure:

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

  - Start MSDOS

  - gcc -shared Test.c -o message.dll

  - gcc Hello.c -o hello.exe

  - hello.exe



    __declspec(dllimport) void hello(void);



    #include <stdio.h> 

    #include <Test.h>


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



    #include <stdio.h> 

    #include <windows.h>


    int main () {


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

      int i = 0;

      scanf("Enter number: %i", &i);


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

      HANDLE  hdll  = LoadLibrary("message.dll");            //LoadLibrary.


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

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

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



      return 0;