Beranda > Delphi > ExecuteProcess – with Command Line Support

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;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: