DLL
extern "C" __declspec(dllexport) int PrinterTest(int i)
{
return 0;
}调用程序
/////////////////////////////////////////////////////////////
typedef int (STDMETHODCALLTYPE * IC_Print)(int);
HINSTANCE gLib32_DLL;
IC_Print icprint;
gLib32_DLL = LoadLibrary ("CS.dll");
if (gLib32_DLL == NULL)
{
MessageBox("-1");
return;
}
//exit IC
         icprint = (IC_Print) GetProcAddress (gLib32_DLL,"PrinterTest");
if (icprint == NULL)
{
FreeLibrary (gLib32_DLL);
MessageBox("-2");
}
else
MessageBox("0"); int ret = -1;
ret = icprint(1);
编译没问题,可是运行时,当我打开文档是,总出现   
  Debug   Error!   
    
  File:i386\chkesp.c   
  Line:42   
    
  The   value   of   ESP   was   not   properly   saved   across   a   function   call.   This   is   usually   a   result   of   calling   a   funtion   declared   with   one   calling   convention   with   a   function   pointer   declared   with   a   different   calling   convention.

解决方案 »

  1.   

    调用执行到
    ret  =  icprint(1);  
     
     
    总出现帖子里错误
      

  2.   

    这个函数默认肯定是__cdecl的,函数实现未给出调用限定
    -----
    extern "C" __declspec(dllexport) int PrinterTest(int i)
    {
    return 0;
    }
    ----
    但是你在调用时候使用的是__stdcall,
    typedef int (STDMETHODCALLTYPE * IC_Print)(int);--------------------
    这两种调用方式是不同的,上边的是函数结束的时候退栈,下边的是在调用的地方由调用者退栈,你的情况就是函数被退了2次栈,导致栈不平衡,肯定出错,
    解决方法:统一dll和使用的地方的调用限定,最好在DLL中加上函数的调用限定。
      

  3.   

    那怎么限定extern "C" __STDMETHODCALLTYPE (dllexport) int PrinterTest(int i)
    {
    return 0;
    }对么
      

  4.   

    STDMETHODCALLTYPE调用限定放到返回类型后,函数名前。
      

  5.   

    extern "C" __declspec(dllexport) int STDMETHODCALLTYPE PrinterTest(int i)
    {
    return 0;
    }还是extern "C" (dllexport) int STDMETHODCALLTYPE PrinterTest(int i)
    {
    return 0;
    }
      

  6.   

    报错: error C2144: syntax error : missing ';' before type 'int'
      

  7.   

    问题解决办法:
    typedef int (STDMETHODCALLTYPE * IC_Print)(int);
    改为:
    typedef int ( * IC_Print)(int);大家快点跟贴
    准备散分