![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Fetch the command line arguments to the applicationAnswer:You can use two different techniques to solve this problem. Method 1: The first, and probably the easiest, method is to call the VCL ParamStr() function. You can use the ParamCount() function to determine how many command line arguments were passed to the program. ParamStr takes an integer argument and returns an AnsiString object. ParamStr will return the full path of the executable if you pass a value of 0. Passing 1 returns the first command line argument after the program name. Passing 2 returns the second argument, and on and on. To practice, start a new project and place 5 labels onto the main form. Add this code to the form's constructor: Label1->Caption = ParamStr(0); Label2->Caption = ParamStr(1); Label3->Caption = ParamStr(2); Label4->Caption = ParamStr(3); Label5->Caption = ParamStr(4); Run the program. On my system I see: E:\CBUILDER\PROJECTS\PROJECT1.EXE Labels 2 through 5 are blank because I didn't pass any arguments to the program. Close the program and select Run | Parameters from the C++Builder menu. Type in a few arguments(-debug -testing -param) and run the program again. You should see: E:\CBUILDER\PROJECTS\PROJECT1.EXE -debug -testing -param Note: ParamStr is smart about directories with spaces in their names. To prove this, copy the EXE to the Program Files directory and run it from there. Observe that ParamStr(0) contains the entire path, including the space. Method 2: The second method is to call the API GetCommandLine function. GetCommandLine takes no arguments and returns a C style char * containing the entire command line. You will have to parse the string to extract the commands. Label5->Caption = AnsiString(GetCommandLine()); On my system, Label5 contains (note the added quotes): "E:\CBuilder\Projects\Project1.exe" -debug -testing -param | ||||||
All rights reserved. |