小弟在自己写的一个dll中调用另一个dll,结果在dll1装入dll2后,一到调用dll2的函数的时候就出现以下错误:Debug error!Program:...\Test1.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 on calling convention with a function pointer declared with a different calling convention.并且,小弟将dll2文件删除,还是出现这个错误信息,而且此时检查LoadLibrary("MyDll")发现其返回值并不为空,请教各位高手该如何处理,多谢,马上给分。

解决方案 »

  1.   

    用过了 "d:\\Mydll.dll"也不行
      

  2.   

    exe和dll中的函数的调用方式不一致。
    举个例子:如果你的DLL导出函数定义是int f(){  },那么把它换成int WINAPI f(){  }试试看。
      

  3.   

    可能是是_stdcall和_cdcel问题。
    看看你的调用和声明是否一致。看看你的工程属性,project-->setting-->c++->code generation-->calling convention, 是__stdcall,还是__cdcel
      

  4.   

    To webber84(***闭关修炼中***) :
    WINAPI加上了也不行阿
    To tserpent(Tang):
    calling convention我已经统一了,可还是不行
    To flyelf(空谷清音:
    请问函数入栈方式如何统一呢我的代码如下,是不是本身有问题呢:
    我用vb程序静态调用MyDll1.dll,在MyDll1.dll中调用MyDll2.dll 
    MyDll1.h
     
    #ifdef MYDLL1_EXPORTS
    #define MYDLL1_API __declspec(dllexport)
    #else
    #define MYDLL1_API __declspec(dllimport)
    #endif
     
    int LoadDll();
    void ExitDll();
    int DoCalculate(int Code); 
    MyDll1.cpp
     
    #include "stdafx.h"
    #include "MyDll1.h"
     
    static HINSTANCE DLLInst = NULL; //动态库句柄
    typedef int(*CALCULATE)(int);
    CALCULATE Calculate;
     
    int LoadDll()
    {
     DLLInst=LoadLibrary("MyDll1.dll");
      if(DLLInst!=NULL)
     {
      Calculate=(CALCULATE)GetProcAddress(DLLInst,"Calculate"); 
      return(0);
     }
     else
     {
      ::MessageBox(NULL,"加载动态库失败!", "提示信息", MB_OK | MB_ICONINFORMATION);
      return(1);
     }
    }
     
    void ExitDll()
    {
     if(DLLInst!=NULL)
      FreeLibrary(DLLInst);
    }
     
    int DoCalculate(int code)
    {
     return Calculate(code);
    }
     
     
    MyDll1.def
     
    LIBRARY  "MyDll1"
     
    EXPORTS
     LoadDll   @1
     ExitDll   @2
     DoCalculate  @3MyDll2.h
     
    #ifdef MYDLL2_EXPORTS
    #define MYDLL2_API __declspec(dllexport)
    #else
    #define MYDLL2_API __declspec(dllimport)
    #endif
     
    int Calculate(int Code);
     
    MyDll2.cpp
     
    #include "stdafx.h"
    #include "SimpleDll.h"
     
    int Calculate(int Code)
    {
     switch(Code)
     {
     case 0:
      return (1);
      break;
     case 1:
      return (2);
      break;
     case 2:
      return (3);
      break;
     case 3:
      return (4);
      break;
     case 4:
      return (5);
      break;
     case 5:
      return (6);
      break;
     case 6:
      return (7);
      break;
     }
    }MyDll2.def
     
    LIBRARY  "MyDll2"
     
    EXPORTS
     Calculate  @1
      

  5.   

    还有就是在def文件中,要把输出的函数说明一下
    最好是用RELEASE编译
      

  6.   

    我已经在def说明了呀
    用release编译也试过了 还是不行
    问题应该还是在那个calling convention上吧
    还有就是小弟一直搞不懂为什么无论MyDll2.dll文件是否存在
    LoadLibrary("MyDll2.dll")的结果都不是Null,这是怎么回事阿
      

  7.   

    返回结果不为null表示load成功了, 你看一下在其他目录下面是否还存在着文件MyDll2.dll
    (例如system32, vc安装的目录等等)
      

  8.   

    多谢各位,问题解决了,但不清楚是怎么回事,一开始就是这样:calling convention是一致的,无论程序目录和系统目录下的dll都被删除,Load都是成功的,可就是每次调用函数计算的时候就出现ESP错误,最后只是将第一个dll改成MFC的,而代码都没什么改变就好了。无论如何现在正常运行,就是高兴阿,给分。