·  MVS 2008 – C – Create – DLL

 

- Info:

  - This tutorial shows how to use MVS 2008 and C to create DLL.

  - It also shows how to create C Console Application which calls that DLL.

  - DLL needs to be in te same directory as executable or Environment Variable Path needs to point to directory with DLL.

 

- Create .dll:

  - Create C File Test.c

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

    Project Defaults – Configuration Type: Dynamic Library (.dll) – OK

  - Debug – Start Without Debugging Ctrl+F5 (It should ask you to specify executable file)

 

    Test.c

    #include <stdio.h>

    

    __declspec(dllexport) void hello() {

      printf("Hello from DLL\n");

    }

 

- Create Test Application:

  - Create C Project MyProject2

  - Create C File Test.c

  - Debug – Start Without Debugging Ctrl+F5

 

    Test.c

    #include <stdio.h> 

    #include <windows.h>

    

    int main () {

      

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

    

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

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

              hello();

      return 0;

    }

 

    

 

- Additional Info:

  - If you create both projects in the same solution then both MyProject.dll and MyProject2.exe are placed in the same

    directory D:\Installed\Programming\Workspaces\VisualStudio\Solutions\MySolution\Debug and everything works fine.

  - If projects are created in separate solutions then copy MyProject.dll in the same directory as MyProject2.exe.

  - Remember to define MyProject2 as Default Project.