Dynamic call DLL functions are sometimes normal, sometimes reported to access violation exceptions

Source: Internet
Author: User

Dynamic call DLL functions are sometimes normal, sometimes reported to access violation exceptions


typedef int (add *) (int a,int b);


void Test ()
{
Hinst=loadlibrarya ("Aimdtl.dll");
(Farproc &) add=getprocaddress (HInst, "add");
Add (+);
}


By this code, the Add function is sometimes OK, and sometimes it is reported as an exception to access violation. When you see the hint, the first reaction is a memory anomaly, but what is this causing the memory anomaly?
So I thought of using a variable to receive the return value of the add look.


void Test ()
{
Hinst=loadlibrarya ("Aimdtl.dll");
(Farproc &) add=getprocaddress (HInst, "add");
int Sum=add (UP);
}
As a result, after the Add function has been executed, it is sometimes OK and sometimes the exception is reported to access violation. What is the reason for this? So Google, someone mentioned, may have to add __cdecall. So I changed the code decisively. 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 (+);
}


As a result, after the Add function has been executed, it is sometimes OK and sometimes the exception is reported to access violation. Tangled Up! Why!! At this time, think of in C call DLL function, need to use __stdcall, that is not here also want? Change it now!




typedef int __stdcall (ADD *) (int a,int b);


void Test ()
{
Hinst=loadlibrarya ("Aimdtl.dll");
(Farproc &) add=getprocaddress (HInst, "add");
Add (+);
}




After running, everything OK. The problem has been resolved. But why is that? So I looked back to see __stdcall and __cdecall.


The call protocol of the _stdcall:win32 API, which is called by the function to clean up the stack, all parameters from right to left into the stack, the generated code in the function name has a _ prefix and a @ and the total number of bytes of the parameter (decimal) as a suffix. It does not support mutable parameters, but it produces code that is shorter than _cdecl because there is no code to clean up the stack after each call.


The default calling protocol for _cdecl:c\c++, which is the reason for the caller to clean up the stack, is the function that can use variadic parameters in c\c++, all parameters from right to stack, and the function name in the generated code is prefixed by a _.


At this point is understood, the general DLL functions are used in the extern "C" __stdcall way out of the function interface, in the call DLL function, if there is no add __stdcall and __cdecall is the default call __cdecall, and __ The cdecall is to be cleaned up by the caller, and the stack is not cleaned up in the code, only the function is called, so the


The address of the function may run. Do not run to fly on the OK, and once run to fly there is an exception to access violation. And _stdcall is called by the function to clean up the stack, so the address of the call function will not fly, naturally OK.


The difference between specific __stdcall/__cdecal/__fastcall can be found in http://blog.csdn.net/limenglandon/article/details/8553201.

Dynamic call DLL functions are sometimes normal, sometimes reported to access violation exceptions

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.