当前位置:网站首页>Vs2015 uses loadlibrary to call DLL library
Vs2015 uses loadlibrary to call DLL library
2022-07-29 09:17:00 【Boring ah le】
Reference resources :
//blog.csdn.net/luoyu510183/article/details/93666808
//blog.csdn.net/SonnAdolf/article/details/80339070
//blog.csdn.net/blade1080/article/details/81538384
Loadlibrary Way no longer needed .lib library , When we compile the dynamic library , Two files will be generated ,.dll and .lib ,lib Unlike static libraries , There are just some function symbols , The real realization is dll in . So , Generated when compiling the dynamic library lib You don't need it . however , If you don't use Loadlibrary The way , Then both files need .
Here is a small example :
1、 Compile Library
Let's compile a library file first , Then use Loadlibrary Method call .
mydll.h
#pragma once
#include <windows.h>
#include <iostream>
using namespace std;
#define MY_DLL_EXPORTS
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif
extern "C" MY_DLL_API int open_device(int handle);
extern "C" MY_DLL_API int open_seesion(int hseion);
mydll.cpp
#include "mydll.h"
extern "C" MY_DLL_API int open_device(int handle)
{
cout << "display func: open_device,"<<"handle is:"<< handle << endl;
return 0;
}
extern "C" MY_DLL_API int open_seesion(int session)
{
cout << "display func: open_seesion,"<<"session is:"<< session << endl;
return 0;
}
The default call method is :

After compilation , We use it dumpbin Look at the export symbol :

It means that the function has been exported in the Library , Use loadlibrary It needs to correspond to the exported name .
2、 Compile call app
Be careful , Your application and library files should be in the same directory .
Loadlibrary_test.cpp
#include <iostream>
#include <Windows.h>
#include "mydll.h"
using namespace std;
int main(void)
{
HMODULE hMod = LoadLibrary(TEXT("lib_dll.dll"));// load dll
if (hMod == nullptr)
{
cout << "load dll error!" << endl;
return -1;
}
typedef int(* Open_Device)(int);
typedef int(* Open_Seesion)(int);
Open_Device opendev = (Open_Device)GetProcAddress(hMod, "open_device");// Get function address
if (opendev == nullptr)
{
cout << "load open_device error!" << endl;
return -1;
}
int rv = opendev(1);// Call function through function pointer
Open_Seesion openses = (Open_Seesion)GetProcAddress(hMod, "open_seesion");
if (openses == nullptr)
{
cout << "load open_session error!" << endl;
return -1;
}
rv = openses(2);// Call function through function pointer
FreeLibrary(hMod);
system("pause");
return 0;
}
The phenomenon :

3、 remarks
By the way, let's talk about the relationship between calling convention and function symbols , namely _stdcall, _cdecl. And that is extern "c" Influence on function name symbol .
extern “C” + _stdcall, The function export symbol is : _+ Function name [email protected]+ Number of parameter transfer bytes
because _stdcall Is the callee cleaning up the stack , So the function symbol contains the information of parameters
extern “C” + _cdecl, The function export symbol is : Function name
because _cdecl It is the caller who cleans up the stack , So you only need the function name
Don't use extern Under the circumstances , yes C++ Export mode of , The function symbols are as follows :
:?+ Function name [email protected]@YG+ Return type + Parameters 1 type …[email protected]
If it is _cdecl @YG Turn into @YA
If there is no parameter, the parameter is void, with Z ending , for example :
: ?+ Function name [email protected]@YA+ Return type +XZ
above X Express void type ,H Express int Parameter type
边栏推荐
- Sublime text create page
- STM32 application development practice tutorial: getting to know STM32 for the first time
- 完全背包问题 从朴素到终极
- Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
- Amazfit dial toolbox Online
- 2022电工(初级)考题模拟考试平台操作
- Data is the main body of future world development, and data security should be raised to the national strategic level
- Memory leaks and common solutions
- Leetcode question brushing (6)
- Leetcode:132. split palindrome string II
猜你喜欢

LeetCode刷题(6)

Restful style details
Notes on network principles (five layer network)

2022年山东省安全员C证上岗证题库及答案

Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan

dataframe.to_sql() 一次性插入过多报错

(视频+图文)机器学习入门系列-第1章 引言

Leetcode:132. split palindrome string II

乱打日志的男孩运气怎么样我不知道,加班肯定很多

One click automated data analysis! Come and have a look at these treasure tool libraries
随机推荐
How to choose effective keywords
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
Regular expression verification version number
怎样查询快递物流筛选出无信息单号删除或者复制
多标签用户画像分析跑得快的关键在哪里?
【Unity入门计划】C#与Unity-了解类和对象
State compression DP
What is the key to fast multi tag user profile analysis?
Flutter文本编辑器
C# 使用数据库对ListView控件数据绑定
Notes on network principles (five layer network)
Quaternion and its simple application in unity
(Video + graphics) introduction to machine learning series - Chapter 1 Introduction
201803-3 CCF URL映射 满分题解
Parameter initialization
File upload and expansion
(Video + graphic) introduction to machine learning series - Chapter 3 logical regression
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
Trie树(字典树)讲解
浅谈契约测试