Obtain command line parameters in C ++ Builder

Source: Internet
Author: User
Anyone who has used C language programming knows that in DOS programming, the number of command line parameters and the strings of each parameter can be obtained through the parameters of the main function, such

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

The command line parameters are argc, which are stored in argv [0] ~ In argv [argc-1], where argv [0] is the name (including path) of the executable file, then WindowsProgramHow does one obtain the command line parameters? Anyone who has written Windows programs in C language knows that in windows, the main function winmain () has a parameter lpszcmd, which is the command line parameter. However, we generally do not work in the winmain () function during windows programming. Additionally, lpszcmd is the entire command line. It is not convenient to obtain each parameter. Is there a better way?

If you are using C ++ builder for programming, I would like to congratulate you. Using command line parameters in C ++ builder is very simple. C ++ builder defines three global variables and functions including response line, paramstr (), and paramcount (). With these three variables and functions, you can easily use the command line parameters. First, let's take a look at the definitions of these functions and variables:

Extern package char * cmdline;
Extern package ansistring _ fastcall paramstr (INT index );
Extern package int _ fastcall paramcount (void );

The parameter lpszcmd of cmdline and winmain () is the same, that is, the entire command line parameter. paramcount () is used to obtain the number of command line parameters, but does not include the executable file name, that is equivalent to the argc-1 under DOS, paramster () is used to obtain the value of each command line parameter, paramstr (0) is the executable file name, the actual parameter is paramstr (1 )~ Paramstr (paramcount ()). The following example shows how to use these three function variables.

Place a label on the form to display the total command line parameters, add a ListBox to display each parameter, and add the following to the oncreate event of the formCode:

Label1-> caption = nearline;
For (INT I = 0; I <= paramcount (); I ++)
Listbox1-> items-> Add (paramstr (I ));

Run-> parameters... Modify the command line parameters, and then run the program ...... How is it? Simple enough!

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.