如何调用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;
}