一、win32 dll 定义
在visual studio中创建基本的win32 dll工程,根据向导一步步创建便可;
增加一个def文件如下名字同dll工程名字相同
内容为
LIBRARY NAME.DLL
EXPORTS
functionname1
functionname2
.....
其中fuctionname1是需要提供的dll接口
相关的函数定义方式.
returnType __stdcall fuctionname(prameters....);
retrunType 为返回类型
例如:int __stdcall setParameter(char *host_name,unsigned short port,char *paths);
二、dll 调用
typedef int (__stdcall *lptestdll)(int); //定义dll内的函数原型
HINSTANCE hDll; //DLL句柄
hDll = LoadLibrary(_T("dataAnalyse.dll"));
if (NULL==hDll)
{
MessageBox(_T("DLL加载失败"));
}
lptestdll addFun; //函数指针
lptestdll ptestdll = (lptestdll)GetProcAddress(hDll,"testReturn");
if (NULL==ptestdll)
{
MessageBox(_T("DLL中函数寻找失败"));
}
int x=ptestdll((int)100);
如果函数原型定义为typedef int ( *lptestdll)(int); 没有加上__stdcall 则会在调用函数运行后出现以下错误
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
错误原因:
你定义函数指针原型时出错。
以上只是stdcall类型,如果dll定义的类型和调用的类型必须匹配,否则就会出错!