C + + calls DLL function, error occurred
Run-time Check Failure #0-the value of ESP is not properly saved across a function call. This was usually a result of calling a function declared with one calling convention with a function pointer declared with A different calling convention.
Cause of Error:
An error occurred when you defined the function pointer prototype.
In fact, you do not define the error, but the compiler does not know, because you call the DLL function is a far function, and is a C function, you have to tell the compiler that it is a C function. Then you can add a sentence when you define the function,
Far PASCAL or __stdcall this is OK.
Specific practices:
For example, you would define a function pointer with a NULL return type and a null argument:
typedef void (*lpfun) (void);
This does match the function in our DLL, and it says that we should add a few words to tell the compiler that this is a far C function.
typedef void (WINAPI *lpfun) (void);
typedef void (__stdcall *lpfun) (void);
typedef void (FAR PASCAL *lpfun) (void);
Like the above definition is OK, if using VC + +, then directly with the first definition of OK.
Note that the above is the practice of using MFC (DLL).
If it is a WIN32 DLL, it is necessary to remove WINAPI, __stdcall, far PASCAL of these parameters. Because the default WIN32 DLL is in __cedcall mode, it is not __stdcall mode.
There are too many specific combinations, but the reason to know the error is to declare that the corresponding function does not match.
Calling a function or class member function in a DLL encounters this error:
Run-time Check Failure #0-the value of ESP is not properly saved across a function call. This was usually a result of calling a function declared with one calling convention with a function pointer declared with A different calling convention.
The call rules for function definitions are different from the actual calling rules. If the compiler defaults to __cdecl, and the __stdcall type function uses the __cdecl call rule, the result is a run-time exception due to no error at compile time.
Therefore, setting the calling rule in the function definition resolves the problem.
such as: typedef void (__stdcall Foo) (int a);
Long time did not write code, a few lines of code a day:
typedef Int (*pfun) (HWND hwnd, LPCTSTR Lptext, LPCTSTR lpcaption, UINT utype);
void Ctestprocessmonitordlg::onbnclickedbutton1 ()
{
//Todo:add your control Notification handler code here
//messagebox (Text ("Hello"), Text ("Test"));
//typedef Void (*PFV) ();
hmodule hmod =:: Loadlibraryexw (TEXT ("User32.dll"), NULL, 0);
if (hmod = NULL)
{
pfun pfun= (pfun) GetProcAddress (Hmod, "MessageBoxW");
if (pfun! = NULL)
{
pfun (m_hwnd, Text ("Hello"), Text ("Test"), Mb_yesno);
}
: FreeLibrary (Hmod);
}
}
The following error occurred suddenly:
Run-time Check Failure #0-the value of ESP is not properly saved across a function call. This was usually a result of calling a function declared with one calling convention with a function pointer declared with A different calling convention.
The code changes to the following, then the problem is gone
typedef Int (winapi *pfun) (HWND hwnd, LPCTSTR Lptext, LPCTSTR lpcaption, UINT utype);
void Ctestprocessmonitordlg::onbnclickedbutton1 ()
{
//Todo:add your control Notification handler code here
//messagebox (Text ("Hello"), Text ("Test"));
//typedef Void (*PFV) ();
hmodule hmod =:: Loadlibraryexw (TEXT ("User32.dll"), NULL, 0);
if (hmod = NULL)
{
pfun pfun= (pfun) GetProcAddress (Hmod, "MessageBoxW");
if (pfun! = NULL)
{
pfun (m_hwnd, Text ("Hello"), Text ("Test"), Mb_yesno);
}
:: FreeLibrary (Hmod);
}
}
C + + calls DLL function, error occurred