读书人

一个DLL调用的有关问题

发布时间: 2012-01-23 21:57:28 作者: rapoo

一个DLL调用的问题
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.

[解决办法]
这个函数默认肯定是__cdecl的,函数实现未给出调用限定
-----
extern "C " __declspec(dllexport) int PrinterTest(int i)
{
return 0;
}
----
但是你在调用时候使用的是__stdcall,
typedef int (STDMETHODCALLTYPE * IC_Print)(int);

--------------------
这两种调用方式是不同的,上边的是函数结束的时候退栈,下边的是在调用的地方由调用者退栈,你的情况就是函数被退了2次栈,导致栈不平衡,肯定出错,
解决方法:统一dll和使用的地方的调用限定,最好在DLL中加上函数的调用限定。
[解决办法]
STDMETHODCALLTYPE调用限定放到返回类型后,函数名前。

[解决办法]
下边的一个
[解决办法]
我看错了,是上边的,下边的语法是错的。

读书人网 >VC/MFC

热点推荐