Thursday, 24 June 2010

How to create DLL from CPP file in MS Visual Studio

The process of creating a DLL from a CPP file using visual studio is easy and it requires the following steps.

1. Open visual studio. Go to File->new project -> Visual c++ -> win32 project
2. Give a project name and click ok.
3. Click Application settings and select DLL and click finish.
4. Open the cpp file attached to the project and append your main cpp file to it and add the other source files and header files to the project.
5. Change the name of the main() function in your CPP file to a name you want to use later on while calling the DLL file.
6. If you are using command line arguments and you want to have arguments to be passed to the main() function, load the input pararmeters for the function.
3. Use extern "C" _declspec (dllexport) before the specification of return type of the function.

Example:
Intitially:

int main()
{
//code
}

After following the 3 steps:
extern "C" _declspec (dllexport) int abc(optional input parameters)
{
//code
}
4. Clean up the solution and build it in release mode. You will find your DLL file in the name of the project name in Release folder.

No comments:

Post a Comment