Delphi - CreateProcess - Execute multiple commands

skylla

I want to achieve the following with a CreateProcess() - call:

  1. Change to an svn working-copy
  2. Execute svn commands
  3. Pipe the output to a file

I try this with the following function

procedure TQPortMainForm.CmdMigrationClick(Sender: TObject);
var
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CreateOk: boolean;
  input: String;
begin
  { fill with known state }
  FillChar(StartInfo, SizeOf(TStartupInfo), #0);
  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
  StartInfo.cb := SizeOf(TStartupInfo);

  //debug
  input := 'D: && cd D:\Qport\trunk\Qport\ && ' + SVN_PATH + ' log > C:\users\PhilippKober\UNIQUE_NAME_BLUB.txt';

  CreateOk := CreateProcess(nil, PChar(input), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil,
    nil , StartInfo, ProcInfo);
  { check to see if successful }
  if CreateOk then
    // may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

Simply nothing happens. Has anybody got an idea how to achieve this?

Thanks,

Philipp

EDIT 1: I am using Delphi XE - Build 7601: Service Pack 1

EDIT 2: Here is the solution:

var
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CreateOk: boolean;
  input: String;
  path : String;
  cmd : String;
begin
  { fill with known state }
  FillChar(StartInfo, SizeOf(TStartupInfo), #0);
  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
  StartInfo.cb := SizeOf(TStartupInfo);

  path := 'D:\Qport\trunk\Qport\';
  cmd := 'C:\Windows\System32\cmd.exe';
  //debug
  input := '/C' + SVN_PATH + ' help > C:\users\PhilippKober\UNIQUE_NAME_BLUB.txt';

  CreateOk := CreateProcess(PChar(cmd), PChar(input), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil,
     Pchar(path), StartInfo, ProcInfo);
  { check to see if successful }
  if CreateOk then
    // may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;
David Heffernan

You need to supply an executable file when you call CreateProcess. I guess you are used to calling ShellExecute which is more lax.

You are clearly looking to call cmd.exe so you should add that to the command line. Rather than changing the working directory after cmd.exe has started, use the lpCurrentDirectory parameter of CreateProcess to do that. You will also need to pass the /C option to cmd.exe to make it close once the command has completed.

So you need to change input to be this:

input := GetEnvironmentVariable('COMSPEC') + ' /C ' + SVN_PATH + 
  ' log > C:\users\PhilippKober\UNIQUE_NAME_BLUB.txt';

I use GetEnvironmentVariable('COMSPEC') as a means to obtain the path to the command interpretor.

And then call CreateProcess like this:

CreateProcess(
  nil, 
  PChar(input), 
  nil, 
  nil, 
  False, 
  CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, 
  nil,
  'D:\Qport\trunk\Qport', 
  StartInfo, 
  ProcInfo
);

It is semantically cleaner to use or to combine flags than +, although it has the same effect for these flags.

One thing to watch out for is that the second parameter must point to writeable memory. That's because CreateProcess may modify that parameter. As it happens, your setting of input will meet that requirement. In any case, a call to UniqueString would be recommended in my view to make it explicit that you are meeting that requirement.

The other thing I see that is missing is code to close the handles that are returned by CreateProcess. Close those handles by doing this in the end:

//WaitForSingleObject(ProcInfo.hProcess, INFINITE); //in case you want to wait for Process to terminate
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute multiple commands after [ test

From Dev

Execute multiple commands after [ test

From Dev

Running a .sh to execute multiple commands

From Dev

execute multiple commands after ssh

From Dev

How to call CreateProcess in Delphi?

From Dev

pass multiple commands for cmd to execute as one block

From Java

Execute combine multiple Linux commands in one line

From Dev

Execute multiple sage commands from php script

From Dev

How to execute multiple CLI commands in a python script?

From Dev

Execute multiple SQL commands at once on R

From Dev

Have puppet conditionally execute multiple commands

From Dev

execute multiple commands in cmd using java

From Dev

Gradle -- execute multiple commands from task

From Dev

execute multiple commands on console through unix shell

From Dev

How to execute multiple commands remotely on few servers?

From Dev

How to execute multiple commands in Bash, some in the background

From Dev

Execute multiple commands over ssh without reconnecting

From Dev

Execute multiple commands with 1 line in Windows commandline?

From Dev

How to execute multiple CLI commands in a python script?

From Dev

execute multiple commands in cmd using java

From Dev

Execute multiple ssh commands with different switch

From Dev

Execute multiple commands in a terminal window from bash

From Dev

Can I execute multiple lines sudo commands?

From Dev

Execute multiple unix commands from SAS

From Dev

Execute multiple remote commands on SSH and close connection

From Dev

Docker execute multiple commands by passing CMD argument

From Dev

C++ execute multiple commands with timer in shell

From Dev

Bash script to execute multiple Unix commands

From Dev

Get PiD of a program created by CreateProcess in Delphi

Related Related

  1. 1

    Execute multiple commands after [ test

  2. 2

    Execute multiple commands after [ test

  3. 3

    Running a .sh to execute multiple commands

  4. 4

    execute multiple commands after ssh

  5. 5

    How to call CreateProcess in Delphi?

  6. 6

    pass multiple commands for cmd to execute as one block

  7. 7

    Execute combine multiple Linux commands in one line

  8. 8

    Execute multiple sage commands from php script

  9. 9

    How to execute multiple CLI commands in a python script?

  10. 10

    Execute multiple SQL commands at once on R

  11. 11

    Have puppet conditionally execute multiple commands

  12. 12

    execute multiple commands in cmd using java

  13. 13

    Gradle -- execute multiple commands from task

  14. 14

    execute multiple commands on console through unix shell

  15. 15

    How to execute multiple commands remotely on few servers?

  16. 16

    How to execute multiple commands in Bash, some in the background

  17. 17

    Execute multiple commands over ssh without reconnecting

  18. 18

    Execute multiple commands with 1 line in Windows commandline?

  19. 19

    How to execute multiple CLI commands in a python script?

  20. 20

    execute multiple commands in cmd using java

  21. 21

    Execute multiple ssh commands with different switch

  22. 22

    Execute multiple commands in a terminal window from bash

  23. 23

    Can I execute multiple lines sudo commands?

  24. 24

    Execute multiple unix commands from SAS

  25. 25

    Execute multiple remote commands on SSH and close connection

  26. 26

    Docker execute multiple commands by passing CMD argument

  27. 27

    C++ execute multiple commands with timer in shell

  28. 28

    Bash script to execute multiple Unix commands

  29. 29

    Get PiD of a program created by CreateProcess in Delphi

HotTag

Archive