[Quick Quick] Visual Studio C/C++ creates Dll (dynamic link library) and calls it

The following examples are all completed in the VS2022 environment.

1. Create a dynamic link library (C++)

1. Create a new dynamic link library project template:

2. After creating a new project MyC++_Dll, the default directory structure is as follows:

3. Add and create a new header file MyC++ _Dll.h and source code file MyC++ _Dll.cpp (used to declare and define function interfaces):

4.MyC++_Dll.h example is as follows:

#pragma once

#ifdef _EXPORTING
#define _DLL_API _declspec(dllexport)
#else
#define _DLL_API _declspec(dllimport)
#endif

#ifdef __cplusplus
class Dll_Class
{
public:
Dll_Class();
~Dll_Class();
virtual int Addition(int a, int b);
};

extern "C"
{
_DLL_API Dll_Class *CreateObject();
_DLL_API void ReleaseObject(Dll_Class *pObject);
_DLL_API int Multiplication_TypeC(int a, int b);
_DLL_API int Addition_TypeC(int a, int b);
}
#endif

Notice:

_EXPORTING is the author’s custom macro, which is used to distinguish whether the dll is being exported or called. To implement the export function, you also need to add a preprocessing definition in the project properties that implement the Dll function (remember to add a semicolon at the end), as shown below:

_declspec(dllexport) is a keyword of VC, which means exporting functions to dll;

_declspec(dllimport) is a keyword of VC, which means calling a function from dll;

_DLL_API is the author’s custom macro, which is used to simplify the code and quickly replace the _declspec(dllimport) and _declspec(dllexport) keywords for function interfaces that need to be imported and exported;

If _EXPORTING is defined, export mode is used, otherwise calling mode is used;

The __cplusplus macro indicates that the C++ compilation environment is used. #ifdef __cplusplus can be used to determine whether the current C++ compilation environment is used;

extern “C” {} means that the functions in the code block are compiled in C language.

5.MyC++_Dll.cpp example is as follows (specific function implementation):

#include "pch.h"
#include "MyC++ _Dll.h"

Dll_Class::Dll_Class() {

}

Dll_Class::~Dll_Class() {

}

int Dll_Class::Addition(int a, int b) {
return a + b;
}

Dll_Class* CreateObject() {
Dll_Class* pClass = new Dll_Class();
return pClass;
}

void ReleaseObject(Dll_Class* pObject) {
if (pObject) {
delete pObject;
pObject = NULL;
}
}

int Multiplication_TypeC(int a, int b) {
return a * b;
}

int Addition_TypeC(int a, int b) {
return a + b;
}

6. Compile the code to generate files such as lib and dll, as shown below:

2. Create dynamic link library (C)

1. Create a new empty project MyC_Dll:

2. Add and create a new header file MyC_Dll.h and source code file MyC_Dll.cpp (used to declare and define function interfaces):

3.MyC_Dll.h example is as follows:

#ifndef _MYC_DLL_HEADER_FILE
#define _MYC_DLL_HEADER_FILE

#ifdef _EXPORTING
#define _DLL_API __declspec(dllexport)
#else
#define _DLL_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
_DLL_API int Addition_TypeC(int a, int b);
_DLL_API int Multiplication_TypeC(int a, int b);
}
#endif
#endif

Notice:

_EXPORTING is the author’s custom macro, which is used to distinguish whether the dll is being exported or called. To implement the export function, you also need to add a preprocessing definition in the project properties that implement the Dll function (remember to add a semicolon at the end), as shown below:

_declspec(dllexport) is a keyword of VC, which means exporting functions to dll;

_declspec(dllimport) is a keyword of VC, which means calling a function from dll;

_DLL_API is the author’s custom macro, which is used to simplify the code and quickly replace the _declspec(dllimport) and _declspec(dllexport) keywords for function interfaces that need to be imported and exported;

If _EXPORTING is defined, export mode is used, otherwise calling mode is used;

The __cplusplus macro indicates that the C++ compilation environment is used. #ifdef __cplusplus can be used to determine whether the current C++ compilation environment is used;

extern “C” {} means that the functions in the code block are compiled in C language.

4.MyC_Dll.cpp example is as follows (specific function implementation):

#include "MyC_Dll.h"

int Addition_TypeC(int a, int b) {
return a + b;
}

int Multiplication_TypeC(int a, int b) {
return a * b;
}

5. You must modify the configuration type in the project properties to dynamic library (.dll) to generate dll, as shown below:

6. Compile the code to generate files such as lib and dll, as shown below:

3. Implicitly calling Dll

1. Create an empty project Call_dll:

2. Add and create a new source code file Call_Dll.cpp:

3.Call_Dll.cpp example is as follows (specific call function interface implementation):

//Define whether to compile in C language
//#define USING_C

#ifdef USING_C
#include <stdio.h>

#include "../MyC_Dll/MyC_Dll.h"
#pragma comment(lib, "../Debug/MyC_Dll.lib")
#else
#include <iostream>
using namespace std;

#include "../MyC + + _Dll/MyC + + _Dll.h"
#pragma comment(lib, "../Debug/MyC++_Dll.lib")
#endif

int main()
{
#ifdef USING_C
int a = 8, b = 6;

printf("Addition_TypeC result = %d\\
", Addition_TypeC(a, b));
printf("Multiplication_TypeC result = %d\\
", Multiplication_TypeC(a, b));
#else
int a = 8, b = 6;
Dll_Class* pMyDll_Object = CreateObject();

cout << "My_Dll_Class.Addition result = " << pMyDll_Object->Addition(a, b) << endl;
cout << "Addition_TypeC result = " << Addition_TypeC(a, b) << endl;
cout << "Multiplication_TypeC result = " << Multiplication_TypeC(a, b) << endl;

ReleaseObject(pMyDll_Object);
#endif

return 0;
}

Notice:

(1)#pragma comment(lib, Lib file) and #include “dll header file” must ensure the real path.

(2) The author here uses #ifdef USING_C to choose C language or C++ style (the main difference between the two is whether to use C or C++ programming statements, and whether the data structure type of calling Dll is unique to C++ )transfer.

4.Dll file is copied to the target path generated by the Call_Dll project, as shown below:

Note: Implicit calling requires that the dll file and the exe file be in the same directory or the dll file is registered in the global running environment of the operating system.

5. Compile the code to generate an exe file, and the running result is as follows:

C++:

C:

4. Explicitly call Dll

1. Create an empty project Call_dll:

2. Add and create a new source code file Call_Dll.cpp:

3.Call_Dll.cpp example is as follows (specific call function interface implementation):

//Define whether to compile in C language
//#define USING_C

#ifdef USING_C
#include <stdio.h>
#else
#include <iostream>
using namespace std;
#endif
#include <Windows.h>

int main()
{
HINSTANCE hdll;

#ifdef USING_C
typedef int (*_Fun_Addition_TypeC)(int, int);
typedef int (*_Fun_Multiplication_TypeC)(int, int);

hdll = LoadLibrary(L"MyC_Dll.dll");
if (hdll == NULL) {
printf("Error: Load dll fail.\\
");
return -1;
}

_Fun_Addition_TypeC Addition_TypeC;
_Fun_Multiplication_TypeC Multiplication_TypeC;

Addition_TypeC = (_Fun_Addition_TypeC)GetProcAddress(hdll, "Addition_TypeC");
Multiplication_TypeC = (_Fun_Multiplication_TypeC)GetProcAddress(hdll, "Multiplication_TypeC");

if (Addition_TypeC == NULL || Multiplication_TypeC == NULL) {
printf("Error: Call functions fail.\\
");
return -2;
}

int a = 8, b = 6;
printf("Addition_TypeC result = %d\\
", Addition_TypeC(a, b));
printf("Multiplication_TypeC result = %d\\
", Multiplication_TypeC(a, b));
#else
class_Dll_Class
{
public:
//_Dll_Class();
//~_Dll_Class();
virtual int Addition(int a, int b)=0;
};

typedef _Dll_Class* (*_Fun_CreateObject)();
typedef void (*_Fun_ReleaseObject)(_Dll_Class*);
typedef int (*_Fun_Multiplication_TypeC)(int, int);
typedef int (*_Fun_Addition_TypeC)(int, int);

hdll = LoadLibrary(L"MyC + + _Dll.dll");
if (hdll == NULL) {
printf("Error: Load dll fail.\\
");
return -1;
}

_Fun_CreateObject CreateObject = (_Fun_CreateObject)GetProcAddress(hdll, "CreateObject");
_Fun_ReleaseObject ReleaseObject = (_Fun_ReleaseObject)GetProcAddress(hdll, "ReleaseObject");
_Fun_Addition_TypeC Addition_TypeC = (_Fun_Addition_TypeC)GetProcAddress(hdll, "Addition_TypeC");
_Fun_Multiplication_TypeC Multiplication_TypeC = (_Fun_Multiplication_TypeC)GetProcAddress(hdll, "Multiplication_TypeC");

if (CreateObject == NULL || ReleaseObject ==NULL || Addition_TypeC == NULL || Multiplication_TypeC == NULL) {
printf("Error: Call functions fail.\\
");
return -2;
}

int a = 8, b = 6;
_Dll_Class* pMyDll_Object = CreateObject();

cout << "My_Dll_Class.Addition result = " << pMyDll_Object->Addition(a, b) << endl;
cout << "Addition_TypeC result = " << Addition_TypeC(a, b) << endl;
cout << "Multiplication_TypeC result = " << Multiplication_TypeC(a, b) << endl;

ReleaseObject(pMyDll_Object);
#endif

if (hdll == NULL) {
FreeLibrary(hdll);
hdll = NULL;
}

return 0;
}

Notice:

(1) LoadLibrary (dll file path) must ensure a real path.

(2) The author here uses #ifdef USING_C to choose C language or C++ style (the main difference between the two is whether to use C or C++ programming statements, and whether the data structure type of calling Dll is unique to C++ )transfer.

4. Copy the Dll file to the path specified by LoadLibrary (dll file path), as shown below:

Note: Explicit calling requires the dll file path to be the same as LoadLibrary (dll file path). The current example is in the same directory as the exe file.

5. Compile the code to generate an exe file, and the running result is as follows:

C++:

C:

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 193922 people are learning the system