I wanted to load a sample library (i.e. user32) and then using one exported function of that library like messageboxw to show a message to the user. My programs works fine, it shows the message but when I click on a button to close the program, it shows the following message:
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.
My source code:
#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 available\nDo you want to try again?", L"Account Details", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
}
}
FreeLibrary(handle_user32_dll);
return 0;
}
Where I did mistake and how can I fix this issue?