Obtain command line parameters from the main function in Win32 Program

Source: Internet
Author: User

[TranslationArticle, Original from: Workshop /]

In the standard C or Win32 ConsoleProgramIn the main function, they all have two parameters: "argc" and "argv", as shown below:

 
Int main (INT argc, char * argv [])...

These parameters help us to input command line parameters for the program. "Argc" is the number of command line parameters, and "argv" is the array list of input parameters. However, when we create a Win32 GUI program in Visual Studio, winmain becomes the entry function of the program, and this function does not have the "argc" and "argv" parameters, how can we input command line parameters to Windows programs? In Windows, how does one obtain these input parameters?

Lpcmdline Parameters

The first solution comes from the winmain function itself. Let's take a look at a typical winmain function declaration:

 
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd )..

As shown in the declaration, the winmain function has a "lpstr (char *)" parameter "lpcmdline". This variable stores all the remaining parts of the command line except the program name. For example, we have an application named "test.exe ".

Test.exe some arguments here

The value of the variable lpcmdline is "some arguments here ". Although this method is not as convenient as "argc" and "argv", it is one of the methods for obtaining command line parameters. We need to write our own program to analyze the lpcmdline string, which increases the complexity of the program.

Windows programCodeBoth the ANSI and Unicode versions are supported. Here, the winmain function is ANSI and wwinmain is Unicode. The main code function created from Visual Studio is named _ twinmain. This function name will be translated into the above two different versions at compilation based on whether the _ Unicode macro is defined in the current project. When translated into the winmain function, the type of the lpcmdline is lpstr, and when translated into the wwinmain function, the type of the lpcmdline is lpwstr, that is, the wide character array]

Getcommandline () function

Another method is to use the getcommandline () API. This function returns the entire command line, which puts the program name (including the absolute path of the Program) and all parameters in a string. This function is very similar to the direct access to the lpcmdline. But one of its advantages is that it can be automatically mapped to the getcommandlinea () or getcommandlinew () function based on the settings of your current project. Therefore, the problem of accessing Unicode command line input is solved. However, it does not provide the number of command line parameters, nor does it have the ability to automatically split parameters into independent variables like argv.

Commandlinetoargvw () Function

The last method I will discuss is the commandlinetoargvw function. This function is only available in the Unicode wide character version and does not have the corresponding commandlinetoargva function. Its statement is as follows:

 
 
Lpwstr * commandlinetoargvw (maid, int * pnumargs)

This function is as simple as 'argc '/'argv', but it does not directly access argc and argv variables in Windows programs. As shown in the declaration, the function accepts two parameters: one is the Unicode name line string to be parsed, and the other is the pointer to the integer variable. The function saves the number of parameters to this integer variable when returning the result.

The function returns a string array similar to 'argv. Let's take an example:

 
 
 
Int winapi winmain (hinstance, hinstance previnstance, lpstr lpcmdline, int nshowcmd) {lpwstr * szarglist; int argcount; szarglist = commandlinetoargvw (getcommandline (), & argcount ); if (szarglist = NULL) {MessageBox (null, l "unable to parse command line", l "error", mb_ OK); return 10 ;}for (INT I = 0; I <argcount; I ++) {MessageBox (null, szarglist [I], l "Arglist contents", mb_ OK);} localfree (szarglist); Return 0 ;}

As shown above, through this function, we can obtain the number of command line parameters (argc) and a string list (argv ). The only thing to note here is that the function will allocate a piece of memory to the returned parameter list. After the list is used up, we need to manually release the memory; otherwise, memory leakage will occur.

Edit: variables _ argc and _ argv

In addition, Microsoft provides global variables _ argc and _ argv. These two variables are automatically assigned values at runtime by windows. Here, _ argv has ASCII and Unicode versions, respectively _ argv and _ wargv. To use these two global variables, you must reference the "stdlib. H" header file (this header file has been referenced in windows. h ). To automatically select ascii or Unicode version variables based on project settings, we can reference the "tchar. H" header file and access the _ targv variable. The Code is as follows:

Extern int _ argc;

Extern tchar * _ targv;

This method is the simplest of all methods. However, the cost is that the program needs to link to the VC ++ Runtime Library (for example, "msvcrt. dll "). Of course, almost all Windows programs in 99% use this Runtime Library :)


Del. icio. us: Command Line Parameter winmain

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.