Search code examples
delphishellexecutepdftk

Delphi procedure execute program with more than 512 chars


I need to call an external program from Delphi 2006 code with a long list of arguments, specifically to concatenate mutiple PDFs into one file using PDFTK. The full string to be executed has over 512 characters, but both WinExec and ShellExecute have a 512 character limit.

Are there any alternatives to these procedures that have much larger limits?


Solution

  • Just found this @SwissDelphiCentre, which seems to work nicely:

    procedure ShellExecute_AndWait(FileName: string; Params: string);
    var
      exInfo: TShellExecuteInfo;
      Ph: DWORD;
    begin
      FillChar(exInfo, SizeOf(exInfo), 0);
      with exInfo do
      begin
        cbSize := SizeOf(exInfo);
        fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
        Wnd := GetActiveWindow();
        ExInfo.lpVerb := 'open';
        ExInfo.lpParameters := PChar(Params);
        lpFile := PChar(FileName);
        nShow := SW_SHOWNORMAL;
      end;
      if ShellExecuteEx(@exInfo) then
        Ph := exInfo.HProcess
      else
      begin
        ShowMessage(SysErrorMessage(GetLastError));
        Exit;
      end;
      while WaitForSingleObject(ExInfo.hProcess, 50) <> WAIT_OBJECT_0 do
        Application.ProcessMessages;
      CloseHandle(Ph);
    end;