读书人

为啥自己写的dll调用函数会出错详见

发布时间: 2013-10-16 11:29:46 作者: rapoo

为何自己写的dll调用函数会出错,详见内容
// mydll.h文件
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
EXPORT int FA(int a);

// mydll.cpp文件
#include "stdafx.h"
#include "mydll.h"
EXPORT int FA(int a)
{
return a*a;
}

// 测试程序部分代码
typedef int (WINAPI *PFA)(int);

void getResult()
{
int a;
HINSTANCE hLibrary;
PFA FA;

hLibrary=LoadLibrary(TEXT("Testdll.dll"));
if(hLibrary != NULL)
{
FA = (GGG)GetProcAddress(hLibrary, "FA");
a = FA(10);
FreeLibrary(hLibrary);
}
}

当程序执行时提示如下错误信息,经检查是a = FA(10)语句出错,不知道为何?
Run-Time Check Failure #0 - The 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.
编程环境:win7 64位,Visual Studio 2010.


[解决办法]
http://pcedu.pconline.com.cn/empolder/gj/vc/0509/698632_all.html
[解决办法]
VC调用约定是cdcel 你用WINAPI导入, winapi的宏定义是stdcall,所以函数的堆栈被破坏了。lz可以google下,几个函数调用约定的区别。

读书人网 >VC/MFC

热点推荐