6

application1 runs another application2 with 2 parameters eg: (NOTE: application1 is not my program)

application2.exe -d:C:\Program Files\app folder -z:Folder menu\app icons

Problem is... quotes somehow disappeared so instead of 2 parameters application2 will get 5 parameters

Param1=-d:C:\Program
Param2=Files\app
Param3=folder
Param4=-z:Folder menu\app
Param5=app icons

Is there way to retrieve all parameters as single string?

I tried combine parameters in loop

for i:=1 to ParamCount do
parameters=parameters+' '+ParamStr(i);

but it is not good solution, because path can contain also double or triple spaces eg.

Program files\app   folder\ 

cmd.exe can capture all parameters in %* , but it gives wrong results if parameter contains special characters like ^^~@@&& ...

Nafalem
  • 261
  • 5
  • 16
  • 1
    Gentlemen, you are proposing rather clumsy solution because command line is already initialized by RTL to `System.CmdLine` variable. Cross-platform behavior isn't implemented tho. – Free Consulting Aug 15 '14 at 12:50

2 Answers2

9

Call the Windows API function GetCommandLine to retrieve the original command line.

var
  CmdLine: string;
....
CmdLine := GetCommandLine;

You'd better hope that you never need to work with a file whose name contains a dash following a space! Trying to persuade the author of the other application to fix their programming would be prudent.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
7

If you are ending up with 5 parameters instead of 2, it is because application1 DID NOT quote the parameters to begin with, eg:

application2.exe "-d:C:\Program Files\app folder" "-z:Folder menu\app icons"

Or:

application2.exe -d:"C:\Program Files\app folder" -z:"Folder menu\app icons"

Assuming you cannot get the author of application1 to fix the parameters, you will have to use GetCommandLine() to retrieve the original command line and parse it yourself however you need. Just keep in mind that unless ALL of your parameters start with -, or unless the parameters are quoted, then you are likely going to run into issues parsing spaces. That is what quotation marks are meant to handle.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770