程序可以正常编译通过,但运行时在调用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函数的问题还是动态调用参数传递的问题?

解决方案 »

  1.   

    typedef int (WINAPI * INITAPI)(DWORD, DWORD, LPCTSTR);
    INITAPI lpFarProc;
    lpFarProc = (test)GetProcAddress(hComm,"IFInitInterface");
    }
    if (lpFarProc(5, 1, "211.138.240.238 7890 5000")) 

             AfxMessageBox("初始化成功!");
    }
    ......
    FreeLibrary(hComm);
      

  2.   

    要看IFInitInterface的原型,不过一般的dll导出函数都是__stdcall格式的,你试试
    hInitAPI = (int (__stdcall*)(DWORD, DWORD, LPCTSTR))lpFarProc; //指针类型转换}
      

  3.   

    应该是这样吧
    (*lpFarProc)(5, 1, "211.138.240.238 7890 5000") ;
      

  4.   

    IFInitInterface在H文件中的原型是:
    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 *)
      

  5.   

    lpFarProc = (test)GetProcAddress(hComm,"IFInitInterface");
    ----》
    lpFarProc = (INITAPI)GetProcAddress(hComm,"IFInitInterface");
      

  6.   

    bohut(伯虎):
    已经是后面的这种调用方式了。
      

  7.   

    你的hInitAPI 也要定义成__stdcall的函数指针
    int (__stdcall* hInitAPI)(DWORD, DWORD, LPCTSTR);
    不过,bohut(伯虎)的也应可以,程序应该没问题了
      

  8.   

    问题可能出在
    if (hInitAPI(5, 1, "211.138.240.238 7890 5000")) 
    你能保证你的参数的值符合DLL中函数的要求吗?