Hi

Hi
i'm trying to run CMD keep it running and pass parameters to it tired this

begin
ShellExecute(Handle, nil,
'cmd.exe','
/K "C:\pgsql\bin\pg_ctl" -D "C:\pgsql\bin\/data" -l logfile start',
nil,
SW_SHOWNORMAL);
end;

i got the error:
"filename directory name or volume label syntax is incorrect"



i also tried :

var Source : string;
begin
Source := Trim( '/K "C:\pgsql\bin\pg_ctl" -D "C:\pgsql\bin\data" -l logfile start');

ShellExecute(Handle, nil, 'cmd.exe', PChar(Source) , nil, SW_SHOWNORMAL);
end;

got the same error


Comments

  1. Why ShellExecute? CreateProcess creates processes. Why cmd.exe? Why not start the exe directly?

    ReplyDelete
  2. For some reason he wants to keep cmd running after the command completes.

    ReplyDelete
  3. This is to run the PostgreSQL database server without installing it , to keep the server running the cmd should stay open if i open the cmd and type the parameters it works just fine , but i wanna do that through Delphi

    ReplyDelete
  4. Hamza Benzaoui What happens if you create a batch file (.bat) with your command and run this .bat instead?

    ReplyDelete
  5. Mocte Sandoval i did this and it's working just fine but i'm trying to hide the black screen when the server starts

    ReplyDelete
  6. David Heffernan i didn't understand what are you trying to say , if i dont need cmd.exe what i need ?

    ReplyDelete
  7. You are using cmd.exe simply to start another process. Cut out the middle man and start that process directly with a call to CreateProcess passing the command line for that other process.

    That is, use CreateProcess to execute pg_ctl directly. After all, what do you think cmd is going to do other than call CreateProcess to launch pg_ctl. You might as well do it directly.

    ReplyDelete
  8. I've had issues with CreateProcess for some applications, which was resolved by using ShellExecuteEx. The latter seems to handle paths and environment variables better without doing explicit parameterization.

    ReplyDelete
  9. Lars Fosdal  ShellExecuteEx just calls CreateProcess to create processes. CreateProcess is a tricky function to call that is true. You probably just didn't understand how to handle the first two arguments.

    ReplyDelete
  10. David Heffernan. AFAIK, CreateProcess doesn't handle App paths. https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121.aspx
    ShellExecuteEx does.
    Another thing that is easier with ShellExecuteEx, is UAC elevation.

    ReplyDelete
  11. Elevation without manifest requires ShellExecuteEx. And yes, app paths are part of the shell so again ShellExecuteEx.

    Neither of those concerns are relevant here are they?

    The code in the original post is odd because it has so much indirection. It calls ShellExecuteEx which in turn calls CreateProcess for cmd.exe which in turn calls CreateProcess for pg_ctl.exe. All rather pointless. May as well cut out the first two steps.

    ReplyDelete

Post a Comment