How to send both command line switches and paramstring with TShellExecuteInfo

153 Views Asked by At

I am using Delphi 6 (yes, I know, but I am old school).

I have a problem with TShellExecuteInfo. I want to run this command: C:\delphi\bin\Convert.exe -b-i plus a paramstring (folder and name of file).

If I put the -b-i after the Executeinfo.lpfile then ShellExecuteEx() can't find Convert.exe, and if I put it in Paramstring then Convert.exe can't find the file.

I have spend 3 days on this, so I hope you can help.

BTW, why would Delphi suddenly start to save my file as text?

1

There are 1 best solutions below

5
Remy Lebeau On

You should not be using ShellExecuteEx() for this at all. That function is meant for executing document files, not running applications. You should be using CreateProcess() instead. Simply past the whole command to its lpCommandLine parameter, eg:

procedure ConvertFile(const FileName: string);
var
  Cmd: string;
  Si: TStartupInfo;
  Pi: TProcessInformation;
begin
  Cmd := 'C:\delphi\bin\Convert.exe -b -i ' + AnsiQuotedStr(FileName, '"'); 

  ZeroMemory(@Si, Sizeof(Si));
  Si.cb := Sizeof(Si);

  if not CreateProcess(nil, PChar(Cmd), nil, nil, False, 0, nil, nil, Si, Pi) then
    RaiseLastOSError;
  try
    //...
  finally
    CloseHandle(Pi.hThread);
    CloseHandle(Pi.hProcess);
  end;
end;