How to: Use a DLL – Dynamic Link Library in C++ as Visual Studio 2008 Project

4 comments - This post in romanian

The usage of the programming elements included in DLL’s [...] it is made in C++ applications by respecting the rules stated below.
The usage of a DLL in the C++ programming language in the Visual Studio programming environment, it is made in the following way:

  • Create a C++ project – type: Console application;
  • Define the content for the source file which uses elements included in the DLL;
  • Create the executable file.


Creating a C++ project for developing a DLL file includes:

  • Launching into execution the Visual Studio 2008 programming environment;
  • Creating a new project by launching the command File/New/Project…;
  • Selecting the project type Visual C++/Win32;
  • Selecting the project template in the Templates menu as Win32 Console Application;
  • Attributing the project name in the Name control and the solution name in the Solution Name control; the project will be included as a sub-folder of the Visual Studio 2008 solution;
  • Clicking the OK button launches into execution the Win32 Application Wizard, in which it is selected Application Settings;
  • In Application type it is checked the Console application, while in Additional options it is activated Empty project;
  • The creation of the project it is concluded by clicking Finish.

The definition of the content for the C++ application file it is made by following the next steps:

  • Creating the header file, extension .h;
  • Stating the content for the DLL file including the specification of the import modifier from a DLL file;
  • Creating the C++ source file for using/calling on the elements specified in the header file;
  • Implementing the application functionalities in the C++ source file;
  • Specifying the DLL used for developing the C++ application.

Creating the header file within the defined C++ project involves:

  • Activating the contextual menu associated to the project by right clicking the name of the project in the Solution Explorer window of the Visual Studio 2008 environment;
  • Launching the Add/New Item… command in the contextual menu associated to the project;
  • In the Add New Item dialog box it is selected Categories with Visual C++/Code;
  • In the templates list provided in Templates Header File (.h) is selected;
  • In the Name control it is introduced the name of the header file with .h extension;
  • Clicking the Add button determines the inclusion in the project of a header file, without content though.

The specification of the content for the DLL file used with the indication of the import modifier from a DLL file it is done in the header file and consists in the detailing of the component elements in the DLL. For the four functions implemented at [...], the usage of these functions is done using the DLL import modifier __declspec (dllimport) that allows the import of the four functions from a DLL file. The header file has the following content:

__declspec(dllimport) double Add(double, double);
__declspec(dllimport) double Sub(double, double);
__declspec(dllimport) double Mul(double, double);
__declspec(dllimport) double Div(double, double);

Creating the C++ source file within the defined project consists in:

  • Activating the contextual menu associated with the project in the Solution Explorer window in Visual Studio 2008 environment;
  • Launching the Add/New Item… command in the contextual menu associated to the project;
  • In the Add New Item dialog box it is selected Categories with Visual C++/Code;
  • In the template list provided in Templates it is selected C++ File (.cpp);
  • In the Name control it is entered the name of the C++ source code with the .cpp extension;
  • Clicking the Add button determines the inclusion in the project of a C++ source code file without content.

Implementing the functionalities for the C++ application takes place in the C++ source code file with a .cpp extension, and consists in writing the source file associated to the designed functionalities and the usage of the four functions in CreareDLLProj DLL file, obtained by the procedure described in [...]. The C++ source file has the following content:

#include "UseDLL.h"
#include <iostream>
 
using namespace std;
 
void main(){
	double x,y;
	x = 3.45;
	y = 2.63;
	double s = Add(x,y);
	cout<<"x+y="<<s<<endl;
	double d = Sub(x,y);
	cout<<"x-y="<<d<<endl;
	double p = Mul(x,y);
	cout<<"x*y="<<p<<endl;
	double r = Div(x,y);
	cout<<"x/y="<<r<<endl;
}

The specification of the DLL used for developing the C++ application consists in:

  • Copying the file with .lib extension in the solution sub-folder;
  • Copying in the DLL, the file with the .dll extension, in the Debug sub-folder of the solution;
  • In the contextual menu associated to the project, it is selected Add/Existing Item…;
  • In the Add Existing Item dialog box, it is selected the file with the .lib extension; the clicking on the Add button determines the inclusion at the project level of the .lib file; through this file is accessed the content of the file with the .dll extension.

The connection of the source file with the header file is made through the #include directive placed at the beginning of the source file presented in the example above.
The creation of the application’s executable file is made by compiling the Visual Studio 2008 solution. Its launching into execution it is made from the developing environment or from Windows Explorer.

, , , ,


  1. #1 by Alex B. on July 12th, 2011

    I had a problem with the name of the functions in the new solution project,where you use de UseDLL.h . I get “error LINK2019:unresolved external simbol” and “error LINK1120″. To solve the issue, I was forced to pass the same name to the functions as in CreateDLL.h . Instead of “Adunar, Scadere, Inmultire Impartire” I had to call the functions “Add, Sub, Mul, Div”. Cheers,

    • #2 by Marius on November 8th, 2013

      The function names have been updated.

  2. #3 by Murali on November 8th, 2013

    Alex B. :
    I had a problem with the name of the functions in the new solution project,where you use de UseDLL.h . I get “error LINK2019:unresolved external simbol” and “error LINK1120″. To solve the issue, I was forced to pass the same name to the functions as in CreateDLL.h . Instead of “Adunar, Scadere, Inmultire Impartire” I had to call the functions “Add, Sub, Mul, Div”. Cheers,

    Same here….:)

(will not be published)