Create and call dll dynamic library in C language

foreword

 When C++ calls functions in Dll, if it is inside the enterprise, it must be a three-piece suit (.h\.lib\.dll). In this way, the writer can write a lot of information in the header file, which is convenient for the caller to call. However, once it is used by people from other companies and you don't want others to see it, then the author definitely doesn't want others to see too much information, you just call it.

   Another point is that dll is used when debugging, and lib is used when compiling. The two are tools used at different times.

I write this purely for my own understanding

1. Create dll

First create an empty DLL file, creation method: New - Project - Win32 Console Program - then give a name - OK - Next - Select DLL - Check the blank project. Successfully created a new project named Dll1. There are these things in the project (VS2017)

 Add the header file Dll1.h Add the export function add function in the header file

#pragma once

extern "C" __declspec(dllexport) int add(int a, int b);
//extern "C" 是表示C语言代码,优点通常调用动态库不改变函数名,具体的找相关文献
//_declspec(dllexport) 导出函数,我是这么理解的

 Now we implement it in the Dll1.cpp file

    #include "Dll1.h"
    int add(int a, int b) {
     
    	return a + b;
    }

Then compile, but pay attention to the platform settings

(choose according to requirement)

/*

The difference between x86 and x64:

The exe (executable file) or dll (dynamic link library) compiled by the x86 platform is 32-bit; the corresponding x64 is 64-bit.

If your startup project, that is, the main program (compiled into an exe file) is compiled under the x86 platform, and a project (or dynamic link library) it depends on is compiled from the x64 platform, it will prompt " Could not load file or assembly... or one of its dependencies. An attempt was made to load a malformed program." error. This is because 32-bit programs cannot load 64-bit dlls, let alone call the classes, methods, and objects in them. Conversely, what if the main program is compiled by the x64 platform, but the dll is x86? Answer. . It is also not acceptable!
————————————————
Copyright statement: This article is the original article of the CSDN blogger "Madman's Looking Up", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
Original link: https://blog.csdn.net/admainzl/article/details/101850906

*/

Try to be consistent with the caller to avoid giving in vain.

then click generate

Find Dll1.dll and Dll1.lib in the x64-deBug directory.

 2.1 Call dll dynamic library method one

Dll writing is over, we need to create a new Win32 console program and name it Test

First add Dll1.dll and Dll1.lib under the project (the two files must be placed in the same folder as the software)

Then add the following code to Test.cpp:

//静态调用DLL库的方法
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#pragma comment(lib,"Dll1.lib")//表示链接Dll1.lib这个库。
//(注意数字1和字母l)
/*
更多关于#pragma comment()的去看链接

https://blog.csdn.net/weixin_34405925/article/details/86285203?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163913902116780271950822%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163913902116780271950822&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-86285203.first_rank_v2_pc_rank_v29&utm_term=%23pragma+comment&spm=1018.2226.3001.4187
*/

extern "C" __declspec(dllimport) int __stdcall add(int a, int b);
//__declspec(dllimport) 导入包
// 静态调用DLL库
void main()
{
	int sum = add(10, 20);
	printf("静态调用,sum = %d\n", sum);
}

 2.2 Call dll dynamic library method two

 Only add Dll1.dll under the project does not need Dll1.lib

#include <iostream>
#include<Windows.h>
int main()
{
	HMODULE h = NULL;//创建一个句柄h
	h = LoadLibrary("Dll1.dll");
	if (h == NULL)//检测是否加载dll成功
	{
		printf("加载DLLTest1.dll动态库失败\n");
		return -1;
	}


	typedef int(*AddFunc)(int, int); // 定义函数指针类型
	AddFunc add;
	// 导出函数地址
	add = (AddFunc)GetProcAddress(h, "add");

	
	int sum = add(100, 200);
	printf("动态调用的结果%d\n", sum);
	
	return 0;
}

Summarize:

none

The article comes from C/C++: Windows programming - two methods of calling DLL programs_Mr. Li Si-CSDN Blog_c++ calling dll

Write dll files with C/C++_Vaxy-CSDN Blog_c write dll

Extension 1.Dev c++ tool generates C code to dll file and how to call dll file_Forward no stop -CSDN Blog_devc++ dll

Push 2. Create a dynamic link library (dll)_Unique Libra Blog-CSDN Blog_Create a dynamic link library

3. Detailed Explanation of C/C++ Dynamic Link Library (DLL)

Guess you like

Origin blog.csdn.net/weixin_58503231/article/details/121863927