C + + calls DLL function, error occurred

Source: Internet
Author: User
Tags function definition

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.