How to create and use a Dynamic Link Library in C++ (Visual Studio)



Create DLL Project

open visual studio & click create a new project


select c++, Windows, Library from drop downs from left to right respectively

click Next

give project name, solution name and location




click create

select dynamic link library (DLL) from the application type drop down


click OK

Right click on the project and create c++ header file called ForexLibrary.h

add the following code

#pragma once

#ifdef FOREXLIBRARY_EXPORTS

#define FOREXLIBRARY_API __declspec(dllexport)

#else

#define FOREXLIBRARY_API __declspec(dllimport)

#endif

#include <string.h>

using namespace std;


FOREXLIBRARY_API int __stdcall add(int a, int b);


Right click on the project and create c++ header file called ForexLibrary.cpp

add the following code


#include "ForexLibrary.h"

int __stdcall add(int a, int b)

{

    return a + b;

}

Right click on the project and build the project

ForexLibrary.lib & ForexLibrary.dll will be generated in the debug folder.


Create Client Project

open visual studio & click create a new project


select c++, Windows, Console from drop downs from left to right respectively


Select Console App 

click next 

give project name, solution name and location

click create

select x64 for solution platform

If you are working with the DLL source code and client source code at the same time, it's better if these two projects can be synced.

To do so set the additional includes directories path to the original header

Right click on the ForexLibrary node and open properties

Select All Configurations from the Configuration drop down box

Select Configuration properties -> C/C++ -> General

Edit additional include directories

Select the path to the header file in your DLL project




Include the following code to ForexClient.cpp 

#include <iostream>

#include "ForexLibrary.h"


int main()

{

    add(5,5);

}


Add the DLL import library to your project

Right click on ForexClient node & select the properties

Select All Configurations from the Configurations drop down

Edit Configuration properties -> Linker -> Input , Additonal Dependencies

Set the original path to ForexLibrary.lib which is from the DLL project


Edit Configuration properties -> Linker -> General , Additonal Library Directories


when the application loads, OS search for the DLL
If it is not found in certain system directories, system path or local app directory , application load will fail
Copy the DLL to the client project's build output directory only if it is missing or it has been changed


Copy the DLL in a post build event

Right click on the ForexClient and select properties

Select All Configurations from the Configurations drop down


Edit Configuration properties -> Build Events -> Post-Build Event , Command Line

Add following code 
xcopy /y /d "C:\Users\Sarala\source\repos\ForexLibrary\$(IntDir)ForexLibrary.dll" "$(OutDir)"


Right click on the ForexClient node and Build the project
















Comments

Popular posts from this blog

How to build CppRestSDK using Vcpkg in Visual Studio 2019 (X64 windows & X86 windows)

How to Call Webservice through C++ Dynamic Link Library (DLL) in Visual Studio 2019 Using C++ Rest SDK