本文引用自:VC编程时DLL中导出函数的声明有两种方式
一种方式是:在函数声明中加上__declspec(dllexport);
另外一种方式是:采用模块定义(.def)文件声明,(.def)文件为链接器提供了有关被链接程序的导出、属性及其他方面的信息。
方式一:在函数声明中加上__declspec(dllexport)
/// 在动态链接库程序中
/// 声明动态链接库(**.dll)的对外接口函数TestFuction
extern "C" __declspec(dllexport) int TestFuction(int nType,char *strPath,std::vector &vecData)
{
do anything here
return 0;
}
/// 在外部希望调用动态链接库的程序中
/// 加载动态链接库(**.dll)并调用其对外接口TestFuction
void func()
{
//typedef与函数TestFuction类型相同的函数指针为TESTDLL
typedef int (_cdecl * TESTDLL)(int nType,char *strPath,std::vector &vecData);
HINSTANCE hmod;
//加载动态链接库**.dll
hmod =::LoadLibrary(_TEXT("dll相对路径\\**.dll"));
if(NULL == hmod)
{
TRACE("加载**.dll失败");
}
//定义一个与函数TestFuction类型相同的函数指针lpproc
TESTDLL lpproc;
//搜索**.dll中函数名为TestFuction的对外接口
lpproc = (TESTDLL)GetProcAddress (hmod,"TestFuction");
//如果搜索成功
if(NULL != lpproc)
{
int nType = 0;
char* strPath = "Data";
std::vector vecData;
//通过函数指针lpproc调用**.dll的接口函数TestFuction
int nResult = (*lpproc)(nType,strPath,vecData);
}
//...
//在恰当的时候释放动态链接库**.dll
FreeLibrary(hmod);
}
方式二:采用模块定义(.def)文件声明
首先创建 一个DLL程序(DllTestDef)
在*.cpp中
int __stdcall Add(int numa, int numb)
{
return (numa + numb);
}
int __stdcall Sub(int numa, int numb)
{
return (numa - numb);
}
然后创建一个.def的文件,在里面加上
;DllTestDef.lib : 导出DLL函数
;作者:----
LIBRARY DllTestDef
EXPORTS
Add @ 1
Sub @ 2
最后创建一个测试程序:.cpp文件如下:
#include
#include
using namespace std;
typedef int (__stdcall *FUN)(int, int);
HINSTANCE hInstance;
FUN fun;
int main()
{
hInstance = LoadLibrary("DLLTestDef.dll");
if(!hInstance)
cout
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?