当前位置:网站首页>VS2015采用loadlibrary方式调用dll库
VS2015采用loadlibrary方式调用dll库
2022-07-29 09:05:00 【无聊的阿乐】
参考:
//blog.csdn.net/luoyu510183/article/details/93666808
//blog.csdn.net/SonnAdolf/article/details/80339070
//blog.csdn.net/blade1080/article/details/81538384
Loadlibrary方式不在需要.lib库,当我们编译动态库的时候,会生成两个文件,.dll和.lib ,lib和静态库不同,只是有一些函符号,真正的实现在dll里。所以说,编译动态库时生成的lib是可以不需要的。但是,如果你不采用Loadlibrary的方式,那么两个文件都需要。
下面写了一个小例子:
1、编译库
我们先编译一个库文件,然后采用Loadlibrary的方式调用。
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;
}
默认的调用方式为:

编译完后,我们用dumpbin看一下导出符号:

说明库里边已经把函数导出来了,使用loadlibrary的时候需要和导出名字对应上。
2、编译调用app
注意,你的应用程序和库文件要在同一个目录下。
Loadlibrary_test.cpp
#include <iostream>
#include <Windows.h>
#include "mydll.h"
using namespace std;
int main(void)
{
HMODULE hMod = LoadLibrary(TEXT("lib_dll.dll"));//载入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");//获取函数地址
if (opendev == nullptr)
{
cout << "load open_device error!" << endl;
return -1;
}
int rv = opendev(1);//通过函数指针调用函数
Open_Seesion openses = (Open_Seesion)GetProcAddress(hMod, "open_seesion");
if (openses == nullptr)
{
cout << "load open_session error!" << endl;
return -1;
}
rv = openses(2);//通过函数指针调用函数
FreeLibrary(hMod);
system("pause");
return 0;
}
现象:

3、备注
顺便再讲下关于调用约定和函数符号的关系,即_stdcall, _cdecl.还有就是extern "c"对函数名符号的影响。
extern “C” + _stdcall,函数导出符号为 : _+函数名[email protected]+传参字节数
由于_stdcall是被调用方清理堆栈,所以函数符号里面包含了传参的信息
extern “C” + _cdecl,函数导出符号为 : 函数名
由于_cdecl是调用方清理堆栈,所以只需要函数名就可以
不使用extern的情况下,是C++的导出方式,函数符号如下:
:?+函数名[email protected]@YG+返回类型+参数1类型…[email protected]
如果是_cdecl @YG变为@YA
如果没有参数即参数为void,则以Z结尾,例如:
: ?+函数名[email protected]@YA+返回类型+XZ
以上 X表示 void类型,H表示int参数类型
边栏推荐
猜你喜欢
![A little knowledge [synchronized]](/img/4d/4a8beee749328b5867b59740fd7e78.png)
A little knowledge [synchronized]

(Video + graphic) introduction to machine learning series - Chapter 3 logical regression

What is the difference between the pre training model and the traditional method in sorting?

CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo

Count the list of third-party components of an open source project

Leetcode: interview question 08.14. Boolean operation
![[unity entry program] C # and unity - understand classes and objects](/img/bd/13cc90638c6e6ad4a45507b0ed2fb7.png)
[unity entry program] C # and unity - understand classes and objects
[C language] DataGridView binding data

Using logistic regression and neural network to deal with complex binary classification problems

Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
随机推荐
Flowable 高级篇
Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan
2022电工(初级)考题模拟考试平台操作
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
多标签用户画像分析跑得快的关键在哪里?
Leetcode:132. split palindrome string II
SAP sm30 brings out description or custom logical relationship
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
ML.NET相关资源整理
Opencv cvcircle function
On the charm of code language
解决Base64 报错 Illegal base64 character
Database system design: partition
Emmet syntax
C# 使用数据库对ListView控件数据绑定
Sublime text create page
2022 electrician (elementary) test question simulation test platform operation
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
Parameter initialization