当前位置:网站首页>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
边栏推荐
- 201803-3 Full Score solution of CCF URL mapping
- Jetpack Glance? The spring of widgets is coming
- Regular expression verification version number
- Using logistic regression and neural network to deal with complex binary classification problems
- 文件上传及拓展
- Quick sorting (quick sorting) (implemented in C language)
- What are the backup and recovery methods of gbase 8s database
- Could not receive a message from the daemon
- Flowable 基础篇2
- ICMP message analysis
猜你喜欢

Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)

Quick sorting (quick sorting) (implemented in C language)

C language -- 22 one dimensional array

四元数与其在Unity中的简单应用

Information system project manager must recite the quality grade of the core examination site (53)

Emmet syntax
Jetpack Glance? The spring of widgets is coming

How does xjson implement four operations?

1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)

【机器学习】逻辑回归代码练习
随机推荐
Memory leaks and common solutions
No duplicate data in the same field of different databases
One article tells you the salary after passing the PMP Exam
MySQL error summary
Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
Floweable advanced
File upload and expansion
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Trie树(字典树)讲解
How does alternates achieve high-performance publish and subscribe?
[LOJ 6485] LJJ binomial theorem (unit root inversion) (template)
基于C语言实现的NFA确定化和DFA最小化
(Video + graphic) introduction to machine learning series - Chapter 3 logical regression
VS2015采用loadlibrary方式调用dll库
Tesseract图文识别--简单
Flutter文本编辑器
STM32 application development practice tutorial: design and implementation of controllable LED water lamp
Acwing game 59 [End]
(视频+图文)机器学习入门系列-第1章 引言
Leetcode question brushing (6)