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!