With the help of DLL (Dynamic Link Library), we can make our project modular and reduce the development time. A DLL increase the performance of the project and promote the reusability of code. It also helps to the developer to insert and remove the new functionality in the project without any hurdle.




Basically, DLL is a shared library which contains code and data that is used by more than one program at a time and like the executable file DLL cannot run directly. DLL (Dynamic link library) is called by the application.

For example, in windows, the Kernel32.dll file handles memory management, input/output operations and interrupts.

Most important features of the DLL, it is loaded at run time when the application is requested for the DLL functionality and loading time is very low.

In this article, I will teach you how to create the DLL in C++ and how to use this DLL by a C++ application.

Steps to create a DLL in C++

Here I will describe to how to create a DLL project in C++ using the visual studio.

  • Open the visual studio and click on the menu bar to create a new project. See the below Image.

http://aticleworld.com/

  • After selecting the new project, a new dialog box will be open, here select the project type Win32 and give the name to the DLL project.

http://aticleworld.com/

  • On the Overview page of the Win32 Application Wizard dialog box, choose the Next button. After clicking the next button a new window will open. It is Application setting window here we will select the type of the application and click on the finish button to create the DLL project.

http://aticleworld.com/

  • After creating the DLL project you have to add the header files and source file as per your requirements. Here I am adding only one header file (Calculation.h).





http://aticleworld.com/

  • When you have created the header file then write the desired content as per the requirements. Here I am creating a library which performs some basic arithmetic operation like addition, subtraction, and multiplication.

Note: When you have created a DLL project then automatically PROJECTNAME_EXPORTS is defined in preprocessor symbols of the DLL project. In this example, CALCULATIONDLL_EXPORTS is defined when your CALCULATIONDLL DLL project is built.

http://aticleworld.com/

  • Now it’s time to define your class member function in the source file. Here I am defining all member function in CalculationDll.CPP file.




  • Now source and header file are added to the DLL project, to create the DLL and lib just build the DLL project. If everything is fine and your DLL project compiles perfectly without any error then a DLL and .lib file will be generated.

http://aticleworld.com/
http://aticleworld.com/

Steps to create a C++ Application

Here I am creating a c++ application which will use the created DLL.

  • Click on the menu bar to create a new c++ Application project that uses the DLL which I have created just now.

http://aticleworld.com/

  • After selecting the new project a new dialog box will be open, here select the project type Win32 Console Application and give the name to the App project.

http://aticleworld.com/

  • On the Overview page of the Win32 Application Wizard dialog box, choose the Next button. After clicking the next button a new window will open. It is Application setting window here we will select the type of the application and click on the finish button to create the c++ Console Application project.

Now your C++ application project is ready to use the DLL ( Dynamic linking library).

How to Link DLL with c++ Application

Here I am discussing few simple steps to link the DLL project with  c++ Application project.

  • When we have created the DLL and Application then after that we have to reference the DLL to the Application that makes the enable to Application to use the DLL function as per the requirement. To do this, under the CalculationApplication project in Solution Explorer, select the References item. On the menu bar, choose Project, Add Reference.

  • When you click on the Add new Reference then a dialog box will be open which has the lists of the library that you can reference. You need to just click on the check button to the required library. Here only one library is showing in the dialog box.

  • Now your created library is linked with the created Application, but before using the DLL in Application you have to add the DLL header file.There is two way to do this task one way to copy the DLL header file into your Application project, but that might lead to changes in one copy that are not reflected in the other. It is not a safe way so we follow the second way in which we just reference the DLL header file to give the path of original DLL header files in Application project included directories path.

 

After giving the path of DLL header file you able to include the DLL header file in your application. Now it’s time to access the function of the DLL as per the requirement.



In below Application code, I am just calling the DLL calculation function to perform the addition subtraction and multiplication.

 



Video tutorial to show to how to create and use the DLL in C++