Using DLL ensure productivity and/or financial gain in the software development process by reusing the source code written C++. Routines and/or programming components included in a DLL are referenced in the application without the need of their re-implementation in these applications.
To build a DLL in C++ programming language as Visual Studio 2008 project, it proceeds as it follows:
- Creating a C++ project of DLL type;
- Define content for DLL file;
- Creating DLL file.
Creating a C++ project for building a DLL file requires:
- Launching the Visual Studio IDE;
- Create a new project launching command File/New/Project…;
- Select the type of project as Visual C++/Win32;
- Selection of the project template from Templates area as Win32 Console Application;
- Assign the project name in Name control and the solution name in Solution Name control; the project will be included as a sub-folder of the Visual Studio 2008 solution;
- Pressing the OK button launches the Win32 Application Wizard execution in which it selects Application Settings;
- In the Application type it enables the DLL radio button and in Additional options it enables the Empty Project control;
- Creating the project is completed by clicking the Finish button.
Define the DLL file content consists in:
- Create the header file, as .h file;
- Specify the DLL file content with export modifier in a DLL file;
- Creating the C++ source file to build the elements specified in the header file ;
- Implementing the content in C++ source file.
Creating the DLL header file defined in the project involves:
- Enabling the menu associated to the DLL project by right clicking the project name in the Solution Explorer window in Visual Studio 2008 environment;
- Launching the command Add/New Item…;
- In the Add New Item dialog it selects Categories with Visual C++ Code option;
- From the list of available templates in Templates, it selects Header File (.h);
- In Name control the header file name is inserted with .h extension;
- Pressing the button Add determines inclusion in the project of a header file, but without content.
Specifying the DLL content with export modifier in a DLL is done in the file header and consisted of components library building for DLL. For example, consider four functions to implement the basic arithmetic operations: addition, subtraction, multiplication and division. Defining these functions is achieved by DLL export modifier __declspec (dllexport) to allow export of the four functions in a DLL file. CreateDLL.h file has the following content:
__declspec(dllexport) double Add(double, double); __declspec(dllexport) double Sub(double, double); __declspec(dllexport) double Mul(double, double); __declspec(dllexport) double Div(double, double);
Creating the C++ source file defined in the project consists in:
- Enabling the menu associated with the project in the Solution Explorer window in Visual Studio 2008 environment;
- Launching the command Add/New Item…;
- In Add/New Item… dialog, select Categories with Visual C++/Code option;
- From available template list Templates, select C++ File (.cpp);
- In Name control, specify the name of the C++ source file, having .cpp extension;
- Pressing the button Add determines inclusion in the project file source code C++ without content.
Implementing the functions functionalities defined in header file takes place in C++ source file with .cpp extension. It consists in writing the source code associated with arithmetic operations implemented by the four functions. The C++ source file the following content:
#include "CreateDLL.h" double Add(double a, double b){ return a+b; } double Sub(double a, double b){ return a-b; } double Mul(double a, double b){ return a*b; } double Div(double a, double b){ return a/b; }
The link source file to header file is made through the directive #include placed at the top of the source file shown in the example above. Creating DLL file is done by compiling the solution in Visual Studio 2008. This is located in the Debug sub-folder in the solution, on the same hierarchical level sub-folder of the project built in the solution.
DLL using supposes that the DLL file to be retrieved from the Debug sub-folder of the DLL create solution and it is placed in the solution that we use it […].
Thanks, very helpful for rookies as me…