首页 >> 程序开发 >> Delphi

ExecuteProcess - with Command Line Support

作者:lanyus

ExecuteProcess - with Command Line Support

This proc is a Delphi wrapper around
the Win32 API "CreateProcess". It is
a better idea to use this instead of
ShellExecute or other 16-bit functions when you''''re program is to
run in a 32-bit environment

 
procedure ExecuteProcess(FileName: String;
  Params:
String; Show: Integer);
var
 
StartUpInfo: TStartUpInfo;
  ProcessInfo: TProcessInformation;
begin
  if
Params[1] <> '''' '''' then
   
Params := '''' '''' + Params;
  FillChar(StartUpInfo, SizeOf(TStartUpInfo),
0);
 
with StartUpInfo do
  begin
   
cb := SizeOf(TStartUpInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := Show;
 
end;

 
if not (CreateProcess(PChar(FileName), PChar(Params),
          
nil, nil, False, NORMAL_PRIORITY_CLASS,
          
nil, nil, StartUpInfo, ProcessInfo))
 
then
   
RaiseLastWin32Error;
end;

//Modified version of ExecuteProcess that works with
//a Command-Line Parameter

procedure ExecuteProcess( FileName,
                          Params   :
String;
                          Show     : Integer );
var
 
StartUpInfo: TStartUpInfo;
  ProcessInfo: TProcessInformation;
begin
 
Params := Trim(Params);
 
if Params<>'''''''' then
   
FileName := FileName + '''' '''' + Params;

  GetStartupInfo( StartUpInfo );
 
with StartUpInfo do begin
   
cb := SizeOf(TStartUpInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := Show;
 
end;
 
if not (CreateProcess( nil, PChar(FileName), nil,
           
nil, False, NORMAL_PRIORITY_CLASS, nil,
           
nil, StartUpInfo, ProcessInfo))
 
then
   
RaiseLastWin32Error;  //From SysUtils Unit
end;