How to create and use DLL (Dynamic Link Library) in (C++)

How to create and use DLL (Dynamic Link Library) in (C++)

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 the developer to insert and remove the new functionality in the project without any hurdle.




Basically, DLL is a shared library that 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.

The 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 DLL in C++ and how to use this DLL by a C++ application.

Steps to create DLL in C++

Here I will describe 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.

how to create dll in C++

  • 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.

https://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.

https://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).





https://aticleworld.com/

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


#ifdef CALCULATIONDLL_EXPORTS  
#define CALCULATION_API __declspec(dllexport)   
#else  
#define CALCULATION_API __declspec(dllimport)   
#endif  

class  CALCULATION_API CalculationApi
{
public:
    void Addition(void);
    void Subtraction(void);
    void Multiply(void);
};

 

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.

https://aticleworld.com/

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




// CalculationDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include"Calculation.h"
#include<iostream>

using namespace std;

void CalculationApi::Addition(void)
{
    int x = 0;
    int y = 0;

    cout << "Enter the numbers" << endl;

    cin >> x >> y;

    cout << (x + y)<<endl;
}

void CalculationApi::Subtraction(void)
{
    int x = 0;
    int y = 0;

    cout << "Enter the numbers" << endl;

    cin >> x >> y;

    cout << (x - y) << endl;
}

void CalculationApi::Multiply(void)
{
    int x = 0;
    int y = 0;

    cout << "Enter the numbers" << endl;

    cin >> x >> y;

    cout << (x * y) << endl;
}

 

  • Now source and header files 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.

https://aticleworld.com/
https://aticleworld.com/

Steps to create a C++ Application

Here I am creating a c++ application that 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.

https://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.

https://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 the 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 simple steps to link the DLL project with the 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 the below Application code, I am just calling the DLL calculation function to perform the addition subtraction and multiplication.

// CalculationApplication.cpp :

#include "stdafx.h"
#include"Calculation.h"

int _tmain(int argc, _TCHAR*argv[])
{
    Calculation_API obj;
    
    obj.Addition();
    
    obj.Subtraction();
    
    obj.Multiplication();
    
    return 0;
}

 

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

 

Recommended Articles for you,

Reference: Walking through .dll in c++.

4 comments

  1. Very useful!
    For 2017 version VS the video works better than the text, because of the method to include libraries does not work, but the video complements everything.
    Thank you for spend your time on this.

Leave a Reply

Your email address will not be published. Required fields are marked *