如何调用 DLL 中的函数

如何调用LL中的函数

在DLL工程中的cpp中函数定义如下:

extern"C"_declspec(dllexport)

intadd(inta,charb)

{

returna + b;

}

一:显示链接

调用的DLL的主工程的main文件中代码如下:

#include<stdio.h>

#include<Windows.h>

#include<tchar.h>

intmain()

{

HMODULE hModule = NULL;

typedefint(*Func)(inta,intb);

//动态加载DLL文件

hModule = LoadLibrary(_TEXT("..//Debug//FuncDll.dll"));

//获取add函数地址

Func fAdd = (Func)GetProcAddress(hModule,"add");

//使用函数指针

printf("%d/n", fAdd(5, 2));

//最后记得要释放指针

FreeLibrary(hModule);

return0;

}

二:隐式链接:

调用的DLL的主工程的main文件中代码如下:

#include<stdio.h>

#include<Windows.h>

#include<tchar.h>

//先把lib链接进来

#pragmacomment(lib,"..//Debug//FuncDll.lib")

//外部声明的add函数

extern"C"_declspec(dllimport)

intadd(inta,charb);

intmain()

{

//直接调用add函数

printf("%d/n", add(5, 2));

return0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值