程序可以正常编译通过,但运行时在调用Dll中的函数进行传参时出错。
源代码如下://动态调用DLL文件
hComm=LoadLibrary("SMEIDll.dll");
if (hComm==NULL)
{
AfxMessageBox("动态链接库没有找到!!!");
AfxGetMainWnd( )->PostMessage(WM_CLOSE,0,0);
return 1;
}
else
{
//载入DLL文件
DspInfo("定位动态链接库");
//获取函数地址
lpFarProc = GetProcAddress(hComm,"IFInitInterface");
hInitAPI = (int (__cdecl*)(DWORD, DWORD, LPCTSTR))lpFarProc; //指针类型转换}
}
if (hInitAPI(5, 1, "211.138.240.238 7890 5000")) ----这一句出错
{
AfxMessageBox("初始化成功!");
} 错误提示如下:
Debug Error!
Program:..ual C++项目\sx.exe
Module:
File:i386\chkesp.c
Line:42The 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
请教各位大虾,是什么原因造成这个错误?是本身Dll函数的问题还是动态调用参数传递的问题?
源代码如下://动态调用DLL文件
hComm=LoadLibrary("SMEIDll.dll");
if (hComm==NULL)
{
AfxMessageBox("动态链接库没有找到!!!");
AfxGetMainWnd( )->PostMessage(WM_CLOSE,0,0);
return 1;
}
else
{
//载入DLL文件
DspInfo("定位动态链接库");
//获取函数地址
lpFarProc = GetProcAddress(hComm,"IFInitInterface");
hInitAPI = (int (__cdecl*)(DWORD, DWORD, LPCTSTR))lpFarProc; //指针类型转换}
}
if (hInitAPI(5, 1, "211.138.240.238 7890 5000")) ----这一句出错
{
AfxMessageBox("初始化成功!");
} 错误提示如下:
Debug Error!
Program:..ual C++项目\sx.exe
Module:
File:i386\chkesp.c
Line:42The 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
请教各位大虾,是什么原因造成这个错误?是本身Dll函数的问题还是动态调用参数传递的问题?
INITAPI lpFarProc;
lpFarProc = (test)GetProcAddress(hComm,"IFInitInterface");
}
if (lpFarProc(5, 1, "211.138.240.238 7890 5000"))
{
AfxMessageBox("初始化成功!");
}
......
FreeLibrary(hComm);
hInitAPI = (int (__stdcall*)(DWORD, DWORD, LPCTSTR))lpFarProc; //指针类型转换}
(*lpFarProc)(5, 1, "211.138.240.238 7890 5000") ;
BOOL WINAPI IFInitInterface(DWORD dwCodeProtocol,DWORD dwDriverProtocol,LPCTSTR riverParam);to bohut(伯虎):你说的转换方式我试过了,也是一样的问题。to keiy() :换成__stdcall后,连编译都无法通过。报错如下:
error C2440: '=' : cannot convert from 'int (__stdcall *)(unsigned long,unsigned long,const char *)' to 'int (__cdecl *)(unsigned long,unsigned long
,const char *)
----》
lpFarProc = (INITAPI)GetProcAddress(hComm,"IFInitInterface");
已经是后面的这种调用方式了。
int (__stdcall* hInitAPI)(DWORD, DWORD, LPCTSTR);
不过,bohut(伯虎)的也应可以,程序应该没问题了
if (hInitAPI(5, 1, "211.138.240.238 7890 5000"))
你能保证你的参数的值符合DLL中函数的要求吗?