运行时检查失败-不同的调用约定



我想加载一个示例库(即user32(,然后使用该库的一个导出函数(如messageboxw(向用户显示消息。我的程序运行良好,它显示消息,但当我点击按钮关闭程序时,它显示以下消息:

运行时检查失败#0-ESP的值未正确保存通过函数调用。这通常是调用用一个带有函数指针的调用约定声明的函数用不同的调用约定声明。

我的源代码:

#include <Windows.h>
#include <iostream>
#include <functional>
typedef int(*MsgBOX)
(
HWND    hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT    uType
);
int main(int argc, char* argv[])
{
HINSTANCE handle_user32_dll = LoadLibrary(TEXT("User32.dll"));
std::function<int(HWND, LPCWSTR, LPCWSTR, UINT)> MsgBoxInstance;

if(!handle_user32_dll)
{
std::cout << "Dll isn't loaded successfuly." << std::endl;
}
else
{
MsgBoxInstance = reinterpret_cast<MsgBOX>(GetProcAddress(handle_user32_dll, "MessageBoxW"));
if (!MsgBoxInstance)
{
std::cout << "Function didn't resolved.";
}
else
{
MsgBoxInstance(NULL, L"Resource not availablenDo you want to try again?", L"Account Details", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
}
}
FreeLibrary(handle_user32_dll);
return 0;
}

我在哪里犯了错误,如何解决这个问题?

基于注释,我解决了更改typedef签名的问题。

更正的源代码:

#include <Windows.h>
#include <iostream>
#include <functional>
typedef WINUSERAPI int(WINAPI *MsgBOX)
(
_In_opt_ HWND hWnd,
_In_opt_ LPCWSTR lpText,
_In_opt_ LPCWSTR lpCaption,
_In_ UINT uType
);
int main(int argc, char* argv[])
{
HINSTANCE handle_user32_dll = LoadLibrary(TEXT("User32.dll"));
std::function<int(HWND, LPCWSTR, LPCWSTR, UINT)> MsgBoxInstance;
if(!handle_user32_dll)
{
std::cout << "Dll isn't loaded successfuly." << std::endl;
}
else
{
MsgBoxInstance = reinterpret_cast<MsgBOX>(GetProcAddress(handle_user32_dll, "MessageBoxW"));
if (!MsgBoxInstance)
{
std::cout << "Function didn't resolved.";
}
else
{
MsgBoxInstance(NULL, TEXT("Resource not availablenDo you want to try again?"), TEXT("Account Details"), MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
}
}
FreeLibrary(handle_user32_dll);
return 0;
}

最新更新