Get command line arguments in C + + Builder

Source: Internet
Author: User
Tags command line

People who have been programmed in the C language know that programming in DOS allows you to get the number of command-line arguments and the string of each parameter by using the parameters of the main function, such as

int main(int argv ,char *argv[])
{

}

The command-line arguments are ARGC, which are stored in argv[0]~argv[argc-1], where Argv[0 is the name of the executable (including the path), so how does the Windows program get command line arguments? Anyone who has written a Windows program in C is aware that there is a parameter lpszcmd in the main function WinMain () of the Windows program, which is a command-line argument. But we generally do not work in the WinMain () function in Windows programming, plus Lpszcmd is the entire command line, to obtain each parameter is not convenient to use, there is no better way?

If you are using C++builder programming, then I congratulate you that using command-line arguments in C++builder is very simple. C++builder defines three global variables and functions cmdline, Paramstr (), ParamCount (), with these three variables and functions, you can easily use command-line arguments, first let's 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 argument, ParamCount () is used to get the number of command-line arguments, but not the executable file name, which is equivalent to Dos Argc-1,paramster () Used to get the value of each command-line argument, PARAMSTR (0) is the executable file name, and the actual argument is Paramstr (1) ~paramstr (ParamCount ()). Here is an example to illustrate the use of these three function variables.

Place a label on the form to display the total command-line arguments, add a listbox to display each parameter, and then add the following code to the form's OnCreate event:

Label1->Caption=CmdLine;
for(int i=0;i<=ParamCount();i++)
  ListBox1->Items->Add(ParamStr(i));

Then in the menu run->parameters ... To modify the command-line arguments, and then run the program ... What do you think? It's simple enough!

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.