Detailed WinMain function (reprint re edit)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
In Windows applications, we can think of the WinMain () function is the procedure of the entrance, (WinMain) prototype is as follows:
int WINAPI WinMain( 

    HINSTANCE hInstance, 

    HINSTANCE hPreInstance, 

    LPSTR lpCmdLine, 

    int nCmdShow 

); 
Code explanation: 1, Before that, resources exist in memory, so each program, also be regarded as a resource, it has a handle their own, this handle is stored in the variable hInstance. 2, HPreInstance handles a case before the current instance, in the Win32 environment, parameter is always NULL 3, The lpCmdLine parameters for storing the pass to the application, so how to pass parameters to the application? Here are a few examples:
Transfer from the command line input parameters: Notepad hi.txt from the command line, The path here hi.txt this file (a string) this parameter is passed to the application of Notepad by double clicking the mouse operation: we have a hi.txt document now, We in the double click, Then the path to the hi.txt this file is passed as a parameter to the Notepad, That is to say, Double click the operation can pass the file name as a parameter to the specified Windows applications a drag and drop operation: we can use an hi.txt document, Drag to the Notepad program, Then the path to the hi.txt this file is passed as a parameter to the Notepad, That is to say, Drag and drop can pass the file name as a parameter to the specified Windows applications

We can build a program, read parameters: 1) to establish the header file StdAfx.h
#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ 

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 

#define WIN32_LEAN_AND_MEAN   // Exclude rarely-used stuff from Windows headers 

#include <windows.h> 
2) The establishment of test.cpp file
#include "stdafx.h" 

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   // TODO: Place code here.
MessageBox(NULL,lpCmdLine,"CmdLine",0);
return 0;
}
When we drag a file to generate program (double-click the choice to open the use of this procedure is the same), appear: e51d783fc606b2dc838b13454, nCmdShow, The specified window appears for the first time (procedure just started) display at. There are a variety of display, such as:
Do not display, nCmdShow = 0; nCmdShow = 1; normal display, (default 1) minimization shows, nCmdShow = 2; to maximize the display, nCmdShow = 3;

If you need to change the default display mode system, need to specify the nCmdShow in creating a window before the value (through a simple assignment statements, nCmdShow = n) but, actually here to make nCmdShow work, need to take nCmdShow as the argument to the ShowWindow function in addition, here a little to explain the WINAPI macro definition, we look at the WinMain the prototype of the function: int WINAPI WinMain(    HINSTANCE hInstance,    HINSTANCE hPreInstance,    LPSTR        lpCmdLine,    int             nCmdShow ); WINAPI is defined as:
#define WINAPI     __The WinMain function stdcall in VC6.0, actually: int APIENTRY WinMain(    HINSTANCE hInstance,    HINSTANCE hPreInstance,    LPSTR        lpCmdLine,    int             nCmdShow ); The APIENTRY is defined as: #define APIENTRY    WINAPI so in the final analysis is __stdcall, what is this stuff? Here to explain: __stdcall a calling convention (a contract calling function time), the other calling conventions are: __cdecl and __fastcall calling convention will decide the following contents:
1)The stack order function parameters,
2)By the caller or callee to pop the stack parameters,
3)And the generating function method decorated names.

WIN32API uses the __stdcall calling convention
The standard C, C++ uses the __cdecl calling convention __stdcall calling convention: the function parameter is pushed onto the stack from right to left, called by __cdecl memory stack function call clearing transfer parameter before returning agreement: every call its function are included to clear the stack code, so the resulting executable file size than the call the _stdcall function of the. From right to left in a functional stack. Note: member function for variable parameters, always use the __cdecl calling convention (relevant content reference source:) (Appendix: Win32 API and MFC (system on ->)) (Appendix: Win32 API and MFC system (under) -> )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Elvis at November 19, 2013 - 7:59 PM