How to create a DLL library in C using Microsoft Visual Studio IDE and then use it with other languages


How to create a DLL library in C using Microsoft Visual Studio IDE and then use it with other languages


 Step 1: Create a new DLL project from Microsoft Visual Studio IDE, That needs the following steps,

           a) File->New->Project->Visual C++-> Select Win32 Project from Visual Studio installed templates->OK

           b) Win32 Application wizard appears ,Click the application settings->Next ->Select DLL option ,Check the Empty project and Click the finiish button,

           c) From project solution explorer, Select project and right click which will open up popup from that click add->new Item ->C++ File -> Give a File name exmple.c ) for example,

          d) Add code like below,
                    #include<stdio.h>
                    #include<stdlib.h>
                    #ifdef _cplusplus
                    extern "C"
                     {

                    __declspec(dllexport) void example(int example)
                       {

                              //all your code
                       }
                   }
                   #endif

HOW TO CALL THIS DLL in Other projects from Microsoft Visual Studio?

Create a cpp project and call this dll using the follwoing code,
put the dll on "C:\\TestDLLs\\"

For example you created a empty cpp project in visual studio added the follwing code

You have add the main.h file which you have used to create dll code to this project


#include <stdio.h>
#include <stdlib.h>
#include "main.h" //main.h from the dll code

int main()
{
    HANDLE statusdll;
    int (*module)();
    statusdll = LoadLibrary("C:\\TestDLLs\\simpleDLL.dll");
    if(statusdll > (void*)HINSTANCE_ERROR)
    {
        printf("DLL loaded successfuly");
        module = GetProcAddress(statusdll,"modularizedMain");
        modularizedMain();
    }
    else
    {
        printf("Can not able to load library:Error occured");
    }

}