Run-Time Check Failure #0
今天在网上看了一个杀进程的程序,复制过来编译,结果执行完后报错: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.
想了很久还是不知道错误,于是上网查,我要调用的动态库里的函数是远C函数,所以在函数指针定义是要如下格式:
也就是在函数指针前面加上__stdcall就可以了。
这是因为函数定义的调用规则和实际的调用规则不同。如 编译器默认的是__cdecl,而__stdcall 类型的函数却用了 __cdecl 的调用规则,虽然编译时不会报错,但是结果却会出现运行时异常。
所以把在函数定义中进行设置调用规则即可解决此问题。
如: typedef void (__stdcall Foo)(int a);
typedef int (__stdcall*lpE_Pro_KillProcessByName)(IN const char* pchar_param_ProName);
HINSTANCE hDll; //DLL句柄
lpE_Pro_KillProcessByName KillProcessByName;
hDll = LoadLibrary(“WinAPIExS.dll”);
if (hDll != NULL)
{
KillProcessByName=(lpE_Pro_KillProcessByName)GetProcAddress(hDll, “E_Pro_KillProcessByName”);
if (KillProcessByName != NULL)
{
const char procname[]=”Thunder5.exe”;
KillProcessByName(procname);
}
FreeLibrary(hDll);
}
还没有评论,来说两句吧...