WinAPI WinMain, CreateMutex, ShellExecute three functions

Source: Internet
Author: User

The WinMain function is a Windows application entry point that is provided to the user and is prototyped as follows:

int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR ipcmdline, int ncmdshow);

The function has four parameters:

HINSTANCD: Is the handle of the current application long-required instance.

hPrevInstance: Is the handle of an instance on the application. (MSDN: If you want to know if a program has another instance, it is recommended to use a mutex to implement only one instance with a mutex)

Ipcmdline: String, which is a command-line argument.

Ncmdshow:int, which indicates how Windows should be implemented, Windows defines a series of macros to help remember, starting with SW, such as: Sw_show.

The return value is an int type.

If the function succeeds, it will run until the Wm_quit message is received, and it should return the exit value of the wparam parameter of the message. If the function exits before it enters the message loop, it should return 0.

WINAPI is a macro definition:

#define WINAPI __stdcall

__stdcall and __cdecl, __pascal, __fastcall are some of the similar keywords, the details are as follows:

1. They are actually some description of the stack, including the sequence of function arguments and the contents of the stack to be purged (by the caller or by themselves). These keywords are used to tell the compiler what kind of assembly code to produce.

2.VC is called by two functions: __stdcall and __cdecl

The former refers to the mode of Pascal invocation, the latter refers to the time C call mode. Using the Pascal invocation method, the function removes the arguments from the stack, returning to the caller, and using the C call method, the deletion of the parameter is done by the caller.

The WinMain function is called by the system, and the Windows system specifies that the functions called by the system follow the Pascal call method, but the default invocation of the function in the VC is the C-call method, so the WINAPI is explicitly declared before WinMain. In Windows programming you will encounter a lot of similar claims, such as: CALLBACK, WINAPI, Pascal, which are __stdcall on Intel CPU computers.

3.__cdecl is a method of invocation that is used by default for C + + and MFC programs, or it can be specified manually by adding the __CDECL keyword to the function declaration. In the case of __cdecl invocation, the parameters of the function are stacked in a right-to-left order, and the function caller pops the stack to clean up the stacks, so the function that implements the mutable argument can only use that invocation, since every function that uses __cdecl invocation will contain the code to clean up the stack. Therefore, the size of the resulting executable file will be larger, the __stdcall call method is used to invoke the Win32 API function, in the __stdcal invocation mode, the function parameters in the right-to-left order into the stack, the called function before returning to clean the stack of the transfer parameters, the number of function parameters fixed, Since the function body knows the number of arguments passed in, the called function can clean up the stack of passed parameters directly with a ret n instruction before returning, and the __fastcall call method is used for the performance requirements very high, __ Fastcall calls the function from the left side of the two size is not greater than 4 bytes (DWORD) of the parameters are placed in the ECX and edx registers, the remaining parameters are still transferred from right to left, the called function to clean up the stack of the transfer parameters before returning.

ThisCall is used only for "C + +" member functions. This pointer is stored in the CX/ECX register and the parameters are pressed from right to left. ThisCall is not a keyword and therefore cannot be specified by the programmer.

Nakedcall. When other calls are made, if necessary, the compiler generates code to hold the ESI,EDI,EBX,EBP register when the function is entered, and the contents of these registers are generated when the function exits.

Special Note:

1. By default, the __cdecl method is used, so it can be omitted.

2.WINAPI is typically used to decorate functions exported in a dynamic-link library.

3.CALLBACK is used only to modify the callback function.

4. You may have found that VC and BCB under the definition of WINAPI different, then you at least understand why you can not directly from the BCB to reduce the VC DLL for one reason.

Overall, the VC default is the __cdecl way, Win32 API functions are __stdcall way, they are the function of the parameters from right to left into the stack, __cdecl way each function has the code to clean up the stack, you can implement the variable parameter list, but the executable file is larger. The __stdcall method is called by the caller to clean up the stack. The characteristic of __fastcall is that he tells the two parameters on the left side of the parameter to be placed on the register, which is relatively fast. The remaining parameters are still in the stack, and the stack is cleaned by the function itself.

WinAPI WinMain, CreateMutex, ShellExecute three functions

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.