The differences between function call specifications _ cdecl and _ stdcall are clear at a glance (in table format)

Source: Internet
Author: User
Tags at sign

_ Cdecl

_ Stdcall

Default call specifications for C and C ++ programs

To use this call specification, You need to explicitly add _ stdcall (or winapi) text. That isReturn-type_ StdcallFunction-name[(Argument-list)]

InQuiltCall the function (callee) to returnAfterThe stack is adjusted by the caller (caller.

 

1. Caller's function call

2. Execution of the called Function

 

3. Return results of the called Function

 

4. The caller clears and adjusts the stack.

InQuiltCall the function (callee) to returnBefore,QuiltCall the function (callee) to adjust the stack. Figure:

 

1. Caller's function call

 

2. Execution of the called Function

 

3. The called function clears and adjusts the stack.

 

4. Return results of the called Function

Because a piece of code to adjust the stack needs to be generated for each call, the final file generated is large.

Because the code for Stack adjustment only exists in one place (in the code of the called function), the last generated file is small.

The number of parameters of a function is variable (just like that of a printf function), because only the caller knows how many parameters it passes to the called function, and the stack can be adjusted properly at the end of the call.

The number of parameters of a function cannot be variable.

For output functions defined in the C program file, the function name remains unchanged and will not be modified.

For the output function defined in the C ++ program file, the function name will be modified. msdn says underscore character (_) is prefixed to names. I actually tested (vc4 and vc6) and found that it was not that simple.

You can addExtern"C"To remove the function name modification. You can also. DefFile removal function name modification.

Whether it is the output function in the C program file or the output function in the C ++ program file, the function name will be modified.

For the output function defined in the C program file, an underscore (_) is prefixed to the name. the name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list.

The output functions defined in the C ++ program file seem to be more complex, similar to _ cdecl.

It seems that only. DefFile removal function name modification.

_ Beginthread _ cdecl thread function address

_ Beginthreadex and createthread require the thread function address of _ stdcall.

Both parameters are transmitted from right to left.

To enable VB to call, the _ stdcall call specification must be used to define C/C ++ functions. See Microsoft kb153586 article: How to call C functions that use the _ cdecl calling convention.

When you loadlibrary a DLL file and pass the function address obtained by getprocaddress to the above three threads to generate the function, make sure that the output function defined in the DLL file meets the call requirements. Otherwise, the code is compiled into the release version and then run, which may damage the stack and unpredictable program behavior.

Related compilation switch in VC:/GD/GR/GZ. In addition, the/GZ compilation switch added in vc6 can help you check stack problems.

I am also a beginner. If there are any mistakes or supplements, please advise. Thank you.

 

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.