// 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.
#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.
应该是FA = (PFA)GetProcAddress(hLibrary, "FA"); 还有你是动态调用的记得在.def输出接口,否则得不到接口出现那错,是FA为空
// mydll.h文件
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
EXPORT int __stdcall FA(int a);// mydll.cpp文件
#include "stdafx.h"
#include "mydll.h"
EXPORT int __stdcall FA(int a)
{
return a*a;
}// 测试程序部分代码 为win32项目
typedef int (__stdcall *PFA)(int);void getResult()
{
HINSTANCE hLibrary;
PFA FA;
hLibrary = LoadLibrary(TEXT("mydll.dll"));
if(hLibrary != NULL)
{
FA = (PFA)GetProcAddress(hLibrary, "FA");
if(FA != NULL)
{
MessageBox(NULL,TEXT("加载函数成功"),TEXT("提示"),0);
}
else MessageBox(NULL,TEXT("加载函数失败"),TEXT("提示"),0);
FreeLibrary(hLibrary);
}
}
编程环境:win7 64位,Visual Studio 2010.
_stdcall 与 _cdecl 的区别几乎我们写的每一个WINDOWS API函数都是__stdcall类型的,首先,需要了解两者之间的区别: WINDOWS的函数调用时需要用到栈(STACK,一种先入后出的存储结构)。当函数调用完成后,栈需要清除,这里就是问题的关键,如何清除??如果我们的函数使用了_cdecl,那么栈的清除工作是由调用者,用COM的术语来讲就是客户来完成的。这样带来了一个棘手的问题,不同的编译器产生栈的方式不尽相同,那么调用者能否正常的完成清除工作呢?答案是不能。如果使用__stdcall,上面的问题就解决了,函数自己解决清除工作。所以,在跨(开发)平台的调用中,我们都使用__stdcall(虽然有时是以WINAPI的样子出现)。那么为什么还需要_cdecl呢?当我们遇到这样的函数如 fprintf()它的参数是可变的,不定长的,被调用者事先无法知道参数的长度,事后的清除工作也无法正常的进行,因此,这种情况我们只能使用 _cdecl。到这里我们有一个结论,如果你的程序中没有涉及可变参数,最好使用__stdcall关键字。好像用__stdcall关键字比较多