(go) WinMain and main entry function comparison

Source: Internet
Author: User

The main function in C + + language, often with parameter argc, argv, is as follows:
int main (int argc, char** argv)
int main (int argc, char* argv[])//can also be wchar_t or TCHAR
ARGC refers to the number of command line input parameters, argv stores all command-line arguments. At the command line, each of the two argv[n] is separated by a space.
If you enter test.exe a b at the command line
ARGC is 3
Argv[0] to "Test.exe"
Argv[1] to "a"
Argv[2] to "B"

Here's a simple program that can help you see what ARGC is, and what argv specifically represents.
#include <stdio.h>

int main (int argc, char* argv[])
{
for (int i = 0; i < argc; i++)
{
printf ("%s\n", Argv[i]),//by processing argv[0] can get the path of the current executable program.
}
return 0;
}

In addition, the VC environment to the program to pass parameters can be set under the command argument debugging. (The WinMain is the same as below)


WinMain function is as follows:
int WINAPI WinMain (
                 hinstance hinstance,    //handle to current instance
                hinstance hPrevInstance,//handle to previous instance
                 LPSTR lpcmdline,        //command line
                 int ncmdshow             //show State
);

The WinMain function receives 4 parameters, which are passed to the application when the system calls the WinMain function.
The first parameter, HINSTANCE, represents the handle to the instance that the program is currently running, which is a numeric value. When the program runs under Windows, it uniquely identifies the running instance (note that only the running instance of the program has an instance handle). An application can run multiple instances, each running an instance, and the system assigns a handle value to the instance and passes the HINSTANCE parameter to the WinMain function.
The second parameter hprevinstance (MSDN) in the WIN32 environment, this parameter does not work, history left, Hprevinstance=null;
The third parameter, lpCmdLine, is a string terminated with empty (' s '), specifying the command-line arguments to be passed to the application.
Note: Run the parameter, for example at the command line type: Test.exe/install, then the program entrance WinMain, its parameters lpCmdLine received/install parameters. However, as for what you do with this parameter, it is implemented in code, you immediately to the/install parameter to do a "uninstall" processing, is also possible. To implement the installation, this requires adding code to your needs, regardless of the command line parameters.

This is different from the command line int main (int argc, char* argv[])
As above Test.exe a B
lpCmdLine for "a B"

The fourth parameter ncmdshow specifies how the program's window should be displayed, such as Maximize, minimize, hide, and so on. The value of this parameter is specified by the caller of the program, and the application usually does not need to ignore the value of this parameter.

Note: The C language of the Windows API is programmed and does not necessarily require the use of the WinMain entry function.
If you do not use the four parameters of WinMain, it is entirely possible to use main instead of WinMain.
If you use one of the Winmanin parameters in your program, you can also use main instead, but you need to increase the four parameters of WinMain as variables, as follows:
Main ()
{
....
HINSTANCE hinstance;
int icmdshow;
LPTSTR szCmdLine;
Hinstance=getmodulehandle (NULL); Gets the instance handle of the program itself
icmdshow=sw_shownormal;//Defining window Display modes
Szcmdline=getcommandline ();//Get command line string
(Hprevinstance=null; The general program does not use this parameter)
....
}
One thing to note, though, is that the command-line arguments returned by the GetCommandLine () function have the name of the executing program itself, while the WinMain parameter LPSTR lpCmdLine does not contain the name of the executing program itself.
Use main instead of WinMain except that the command-line arguments contain the name of the program itself, the others are not found to be different.

Test it with one of the simplest programs:

version WinMain:

#include <windows.h>
int WINAPI WinMain (hinstance h1,hinstance h2,lptstr cmdline,int cmdshow)
{
MessageBox (NULL,
CmdLine,
"CmdLine",
MB_OK | Mb_iconinformation);

return 0;
}

Because WinMain 's cmdline does not contain a program name, nothing is displayed.

Main version:

#include <windows.h>
int main (int argc,char *argv)
{
LPTSTR CmdLine; To Use this parameter, replace the original parameter with a variable.
Cmdline=getcommandline ();// Gets the command line string, including the program name itself

MessageBox (NULL,
CmdLine,
"CmdLine",
MB_OK | Mb_iconinformation);

return 0;
}

Use main instead of WinMain except that the command-line arguments contain the name of the program itself, the others are not found to be different.

Several programs in the fifth edition of Windows programming were tested to work correctly.

Last additions: For icmdshow can also not be specified in the program, obtained by the system input:

Startupinfo Startupinfo;
memset (&startupinfo,0,sizeof (startupinfo));
Getstartupinfo (&startupinfo);
Icmdshow = (int) startupinfo.wshowwindow;// Get window display mode

In addition, in order to get more accurate cmdline, you can make a self-coding function, but this function, which I have compiled myself, will remove the extra space in the command line:

//This function can be used to return the extra spaces that have been removed.szCmdLine
LPTSTR getcmdline (int argc,char *argv[])
{
int i=0;
int length=0;
char * cmdline;
if (argc<2)
Return TEXT ("");
for (I=1; i<argc; i++)
{
Length=length + strlen (Argv[i]);
}
CmdLine = (char *) malloc (sizeof (char) * (length + argc-1));
strcpy (cmdline,argv[1]);
if (argc>2)
{
for (i=2;i<argc;i++)
{
strcat (CmdLine, "");
strcat (Cmdline,argv[i]);
}
}
Return TEXT (CmdLine);
}

Reference: http://blog.163.com/[email protected]/blog/static/99751486201212345130881/

(go) WinMain and main entry function comparison

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.