Dynamic DLL function call is sometimes normal, and an access violation exception is reported.

Source: Internet
Author: User

Dynamic DLL function call is sometimes normal, and an access violation exception is reported.

Typedef int (add *) (int A, int B );

Void test ()
{
Hinst = loadlibrarya ("aimdtl. dll ");
(Farproc &) add = getprocaddress (hinst, "add ");
Add (1, 2 );
}

Run the following code. The add function is sometimes OK. An access violation exception is reported. The first response was a memory exception, but what caused the Memory exception?
So I want to use a variable to receive the return value of ADD.

Void test ()
{
Hinst = loadlibrarya ("aimdtl. dll ");
(Farproc &) add = getprocaddress (hinst, "add ");
Int sum = add (1, 2 );
}
As a result, after the Add function is executed, it is still sometimes OK. An access violation exception is reported. What is the reason for this? So Google mentioned that _ cdecall may be required. So I decided to modify the code. The new Code is as follows:

Typedef int _ cdecall (add *) (int A, int B );

Void test ()
{
Hinst = loadlibrarya ("aimdtl. dll ");
(Farproc &) add = getprocaddress (hinst, "add ");
Add (1, 2 );
}

As a result, after the Add function is executed, it is still sometimes OK. An access violation exception is reported. Tangle! Why !! In this case, when calling the DLL function in C, you need to use _ stdcall. Is that required here? Change now!

Typedef int _ stdcall (add *) (int A, int B );

Void test ()
{
Hinst = loadlibrarya ("aimdtl. dll ");
(Farproc &) add = getprocaddress (hinst, "add ");
Add (1, 2 );
}

After running, everything is OK. The problem has been solved. But why? So I looked back at the _ stdcall and _ cdecall statements.

_ Stdcall: Win32 API call protocol. The called function clears the stack. All parameters are from right to left in the stack, the generated code contains a _ prefix and a @ and the total number of bytes (decimal) of the parameter as the suffix. It does not support variable parameters, but it produces code that is shorter than _ cdecl because there is no code to clean up the stack after each call.

_ Cdecl: the default call protocol of C \ c ++. The caller clears the stack. This is why variable parameters can be used in C \ c ++, all parameters are from right to the stack. The generated code contains a _ prefix.

This is clear. Generally, functions in the DLL use the extern "C" _ stdcall method to extract the function interface. when calling the function in the DLL, if _ stdcall and _ cdecall are not added, _ cdecall is called by default, and _ cdecall is used by the caller to clear the stack, in the code, the stack is not cleared, but the function is called.

The function address may run. If you do not run the flight, the access violation exception occurs. While _ stdcall is used to clear the stack by the called function, so the address of the called function will not run, and it will naturally be OK.

For details about the differences between _ stdcall/_ cdecal/_ fastcall, see http://blog.csdn.net/limenglandon/article/details/8553201.

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.