Comparison of main winmain entry functions

Source: Internet
Author: User
This article is reproduced, the original address http://blog.csdn.net/jackalfly/article/details/8235225

The main function in C/C ++ often carries the argc and argv parameters, as follows:
Int main (INT argc, char ** argv)
Int main (INT argc, char * argv []) // It can also be wchar_t or tchar
Argc refers to the number of input parameters in the command line. argv stores all command line parameters. Each two argv [N] parameters are separated by spaces.
For example, enter test.exe a B in the command line.
Argc is 3
Argv [0] is "test.exe"
Argv [1] is ""
Argv [2] is "B"

The following simple program can help you see what argc and argv represent.
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
For (INT I = 0; I <argc; I ++)
{
Printf ("% s \ n", argv [I]); // you can obtain the path of the current executable program by processing argv [0.
}
Return 0;
}

In addition, in the VC environment, you can set parameters to be passed to the program under debugging-> command argument. (The winmain mentioned below is also the same)

Under vs2008, right-click the project to be debugged and choose Properties> debugging> command arguments> enter the parameter you want to enter.
The prototype declaration of the winmain function is as follows:
Int winapi winmain (
Hinstance, // handle to current instance
Hinstance hprevinstance, // handle to previous instance
Lpstr lpcmdline, // command line
Int ncmdshow // show state
);

The winmain function receives four parameters that are passed to the application when the system calls the winmain function.
The first hinstance parameter indicates the handle of the Instance currently running the program, which is a numerical value. when a program runs in Windows, it uniquely identifies the running instance (note that only the running program instance has an instance handle ). an application can run multiple instances. Each time an instance is run, the system will assign a handle value to the instance and pass it to the winmain function through the hinstance parameter.
The second parameter, hprevinstance (msdn), does not work in Win32 environment. It is left behind by history. hprevinstance = NULL;
The third parameter, lpcmdline, is a string terminated with an empty string ('\ 0') and specifies the command line parameter passed to the application.
Note: run the parameters. For example, if you type test.exe/install in the command line, the program entry winmain receives the/install parameter from the parameter lpcmdline. However, as for what you do with this parameter, it is implemented by code. You can also perform an "Uninstall" Operation on the/install parameter in real time. To implement the installation, you need to add the code according to your needs, and it has nothing to do with the command line parameters.

This is different from the int main (INT argc, char * argv []) of the command line.
Test.exe A B
The value of lpcmdline is "a B"

The fourth parameter ncmdshow specifies how the window of the program should be displayed, such as maximization, minimization, and hiding. the value of this parameter is specified by the caller of the program. Generally, the application does not need to ignore the value of this parameter.

Note: Windows API programming in C language does not need to use the winmain entry function.
If you do not use the four parameters of winmain, you can simply use main instead of winmain.
If a parameter of winmanin is used in the program, it can also be replaced by main, but four parameters of winmain need to be added as the variable, as shown below:
Main ()
{
....
Hinstance;
Int icmdshow;
Lptstr szcmdline;
Hinstance = getmodulehandle (null); // gets the instance handle of the program itself
Icmdshow = sw_shownormal; // defines the window display mode.
Szcmdline = getcommandline (); // obtain the command line string
(Hprevinstance = NULL; this parameter is not generally used by the Program)
....
}
However, the command line parameters returned by the getcommandline () function contain the name of the execution program, while the winmain parameter lpstr lpcmdline does not contain the name of the execution program.
In addition to whether the command line parameter contains the program name, winmain is replaced by main.

Test it and use the simplest program:

Winmain version:

# Include <windows. h>
Int winapi winmain (hinstance H1, hinstance H2, lptstr using line, int cmdshow)
{
MessageBox (null,
Using line,
"Cmdline ",
Mb_ OK | mb_iconinformation );

Return 0;
}

The program name is not included in the cmdline of winmain, so nothing is displayed.

Main version:

# Include <windows. h>
Int main (INT argc, char * argv)
{
Lptstr skip line; // to use this parameter, replace the original parameter with a variable.
Using line = getcommandline (); // obtain the command line string, including the program name

MessageBox (null,
Using line,
"Cmdline ",
Mb_ OK | mb_iconinformation );

Return 0;
}

In addition to whether the command line parameter contains the program name, winmain is replaced by main.

I tested several programs in "Fifth edition of Windows program design" and they can all run normally.

Note: For icmdshow, it can not be specified in the program, which is obtained by system input:

Startupinfo;
Memset (& startupinfo, 0, sizeof (startupinfo ));
Getstartupinfo (& startupinfo );
Icmdshow = (INT) startupinfo. wshowwindow; // obtain the window display mode.

In addition, you can compile a function to obtain a more accurate cmdline function. However, the self-compiled function removes unnecessary spaces in the command line:

// Using this function, we can return the szcmdline with no extra space removed.
Lptstr getcmdline (INT argc, char * argv [])
{
Int I = 0;
Int length = 0;
Char * character line;
If (argc <2)
Return text ("");
For (I = 1; I <argc; I ++)
{
Length = Length + strlen (argv [I]);
}
Required line = (char *) malloc (sizeof (char) * (Length + argc-1 ));
Strcpy (using line, argv [1]);
If (argc> 2)
{
For (I = 2; I <argc; I ++)
{
Strcat (using line ,"");
Strcat (using line, argv [I]);
}
}
Return text (plain line );
}

The main function in C/C ++ often carries the argc and argv parameters, as follows:
Int main (INT argc, char ** argv)
Int main (INT argc, char * argv []) // It can also be wchar_t or tchar
Argc refers to the number of input parameters in the command line. argv stores all command line parameters. Each two argv [N] parameters are separated by spaces.
For example, enter test.exe a B in the command line.
Argc is 3
Argv [0] is "test.exe"
Argv [1] is ""
Argv [2] is "B"

The following simple program can help you see what argc and argv represent.
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
For (INT I = 0; I <argc; I ++)
{
Printf ("% s \ n", argv [I]); // you can obtain the path of the current executable program by processing argv [0.
}
Return 0;
}

In addition, in the VC environment, you can set parameters to be passed to the program under debugging-> command argument. (The winmain mentioned below is also the same)

Under vs2008, right-click the project to be debugged and choose Properties> debugging> command arguments> enter the parameter you want to enter.
The prototype declaration of the winmain function is as follows:
Int winapi winmain (
Hinstance, // handle to current instance
Hinstance hprevinstance, // handle to previous instance
Lpstr lpcmdline, // command line
Int ncmdshow // show state
);

The winmain function receives four parameters that are passed to the application when the system calls the winmain function.
The first hinstance parameter indicates the handle of the Instance currently running the program, which is a numerical value. when a program runs in Windows, it uniquely identifies the running instance (note that only the running program instance has an instance handle ). an application can run multiple instances. Each time an instance is run, the system will assign a handle value to the instance and pass it to the winmain function through the hinstance parameter.
The second parameter, hprevinstance (msdn), does not work in Win32 environment. It is left behind by history. hprevinstance = NULL;
The third parameter, lpcmdline, is a string terminated with an empty string ('\ 0') and specifies the command line parameter passed to the application.
Note: run the parameters. For example, if you type test.exe/install in the command line, the program entry winmain receives the/install parameter from the parameter lpcmdline. However, as for what you do with this parameter, it is implemented by code. You can also perform an "Uninstall" Operation on the/install parameter in real time. To implement the installation, you need to add the code according to your needs, and it has nothing to do with the command line parameters.

This is different from the int main (INT argc, char * argv []) of the command line.
Test.exe A B
The value of lpcmdline is "a B"

The fourth parameter ncmdshow specifies how the window of the program should be displayed, such as maximization, minimization, and hiding. the value of this parameter is specified by the caller of the program. Generally, the application does not need to ignore the value of this parameter.

Note: Windows API programming in C language does not need to use the winmain entry function.
If you do not use the four parameters of winmain, you can simply use main instead of winmain.
If a parameter of winmanin is used in the program, it can also be replaced by main, but four parameters of winmain need to be added as the variable, as shown below:
Main ()
{
....
Hinstance;
Int icmdshow;
Lptstr szcmdline;
Hinstance = getmodulehandle (null); // gets the instance handle of the program itself
Icmdshow = sw_shownormal; // defines the window display mode.
Szcmdline = getcommandline (); // obtain the command line string
(Hprevinstance = NULL; this parameter is not generally used by the Program)
....
}
However, the command line parameters returned by the getcommandline () function contain the name of the execution program, while the winmain parameter lpstr lpcmdline does not contain the name of the execution program.
In addition to whether the command line parameter contains the program name, winmain is replaced by main.

Test it and use the simplest program:

Winmain version:

# Include <windows. h>
Int winapi winmain (hinstance H1, hinstance H2, lptstr using line, int cmdshow)
{
MessageBox (null,
Using line,
"Cmdline ",
Mb_ OK | mb_iconinformation );

Return 0;
}

The program name is not included in the cmdline of winmain, so nothing is displayed.

Main version:

# Include <windows. h>
Int main (INT argc, char * argv)
{
Lptstr skip line; // to use this parameter, replace the original parameter with a variable.
Using line = getcommandline (); // obtain the command line string, including the program name

MessageBox (null,
Using line,
"Cmdline ",
Mb_ OK | mb_iconinformation );

Return 0;
}

In addition to whether the command line parameter contains the program name, winmain is replaced by main.

I tested several programs in "Fifth edition of Windows program design" and they can all run normally.

Note: For icmdshow, it can not be specified in the program, which is obtained by system input:

Startupinfo;
Memset (& startupinfo, 0, sizeof (startupinfo ));
Getstartupinfo (& startupinfo );
Icmdshow = (INT) startupinfo. wshowwindow; // obtain the window display mode.

In addition, you can compile a function to obtain a more accurate cmdline function. However, the self-compiled function removes unnecessary spaces in the command line:

// Using this function, we can return the szcmdline with no extra space removed.
Lptstr getcmdline (INT argc, char * argv [])
{
Int I = 0;
Int length = 0;
Char * character line;
If (argc <2)
Return text ("");
For (I = 1; I <argc; I ++)
{
Length = Length + strlen (argv [I]);
}
Required line = (char *) malloc (sizeof (char) * (Length + argc-1 ));
Strcpy (using line, argv [1]);
If (argc> 2)
{
For (I = 2; I <argc; I ++)
{
Strcat (using line ,"");
Strcat (using line, argv [I]);
}
}
Return text (plain line );
}

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.