Several call conventions

Source: Internet
Author: User

Today, I accidentally noticed the definition of malloc source code in C ++ and found that I had some doubts about _ cdecl, this has something to do with P/Invoke in C #.

C # When calling WINAPI: [System. Runtime. InteropServices. DllImport ("user32.dll")] one of the parameters involves the call convention, namely CallingConvention.

/* There are three call Conventions: * _ stdcall: _ stdcall is the default call method of the Pascal program. The parameter uses the stack pressure mode from right to left, the called function clears the stack before returning. WIN32 APIs use the _ stdcall call method. This macro definition illustrates the problem: # define WINAPI _ stdcall; according to the C compilation method, the _ stdcall call convention underscores the name of the output function, add the "@" symbol and the number of bytes of the parameter, as shown in. * _ Cdecl call: _ cdecl is the default call method of C/C ++. The parameter uses the pressure stack mode from right to left. The memory stack of the transfer parameter is maintained by the caller. _ Functions agreed by cedcl can only be called by C/C ++. Each function that calls it contains code for clearing the stack, therefore, the size of the executable file is larger than that of the _ stdcall function. Since the parameter memory stack of the _ cdecl call method is maintained by the caller, the variable-length parameter function can (or can only) use this call convention. About the C/C ++ variable length parameter (...) In this article. Because Visual C ++ uses the _ cdecl call method by default, _ stdcall should be used to call DLL in VC. In the C compilation mode, _ cdecl indicates that only the name of the output function is underlined, such as _ functionname. * _ Fastcall: _ fastcall is faster. It passes parameters through the internal registers of the CPU. According to the C compilation method, the _ fastcall call Convention adds the "@" symbol before the output function name, followed by the "@" symbol and the number of bytes of the parameter, such as @ functionname @ number. */
/* DllImport has five naming parameters: * The CharSet parameter indicates the character set used in the entry point. If CharSet is not specified, the default value CharSet. Auto is used. * The CallingConvention parameter indicates the call convention of the entry point. If no CallingConvention is specified, use the default value CallingConvention. Winapi. * The EntryPoint parameter indicates the dll entry point name. If EntryPoint is not specified, the method name is used. * The ExactSpelling parameter indicates whether the EntryPoint must exactly match the spelling of the indicated entry point. If ExactSpelling is not specified, use the default value false. * The PreserveSig parameter indicates whether the method signature should be retained or converted. When the signature is converted, it is converted to a signature with an additional output parameter named retval that has the HRESULT return value and the return value. If PreserveSig is not specified, the default value true is used. */

It can be seen that malloc is a standard library function in C ++. If _ stdcall function () is called, it indicates that winAPI is called.

Related Article

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.