delphishellexecutedelphi-10.1-berlinjedi

Shellexecute and Wait and keep console-window open?


In Delphi 10.1, I need to execute another external console program and wait until the other external console program is terminated. But the other external console-window should remain open after it has done its work.

Right now I use this JCL (JEDI) function:

ThisShellExecResult := JclShell.ShellExecAndWait(
  ThisProgram,     // Filename
  ThisParameters,  // Parameters
  '',              // Verb (Operation)
  ThisShowCommand, // ShowCommand (SW_SHOW or SW_HIDE)
  ThisDirectory,   // Directory
  );

This function works well as it waits until the external console-program is terminated.

But how can I make the external console-program keep its console-window open?

I have often seen a /k parameter mentioned to keep a console window opened.

But how could it fit in the context of this function?


Solution

  • As my question was "But how could it fit in the context of this function?" (where parameters are passed to the external program, as in the code example from the question), here is the answer:

    ThisShellExecResult := JclShell.ShellExecAndWait(
      'cmd.exe',       // Filename
      '/K ' + JclStrings.StrQuote(ThisProgram, '"') + ' ' + ThisParameters,  // Parameters
      '',              // Verb (Operation)
      ThisShowCommand, // ShowCommand (SW_SHOW or SW_HIDE)
      ThisDirectory    // Directory
      );
    

    This works, as I have verified it by testing.