unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TExecuteWaitEvent =
procedure(
const ProcessInfo: TProcessInformation;
var ATerminate: Boolean)
of object;
type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure ExecuteFile(
const AFilename:
String;
AParameter, ACurrentDir:
String; AWait: Boolean;
AOnWaitProc: TExecuteWaitEvent=nil);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender : TObject);
begin
//Showmessage(ParamStr(1)+ #13 + ParamStr(2));
if ParamCount > 0
then begin
if (UpperCase(ParamStr(1)) = '
/DEBUG')
then begin
// Form1.Visible:=true;
Showmessage('
Zum versteckten Starten von Programmen verwende:' + #13 + '
vStarter.exe /s "c:\dokumente und einstellungen\user\desktop\test.exe" <optionale Programmparameter>');
Application.Terminate;
end;
if (UpperCase(ParamStr(1)) = '
/S')
and (UpperCase(ParamStr(2)) <> '
')
then
if (UpperCase(ParamStr(3)) <> '
')
then begin
ExecuteFile(ParamStr(2), ParamStr(3), '
', true);
Application.Terminate;
end
else begin
ExecuteFile(ParamStr(2), '
', '
', true);
Application.Terminate;
end;
end;
if ParamCount = 0
then begin
showmessage('
vStarter wurde ohne bzw. mit inkorrekten Parametern aufgerufen und nun beendet.');
Application.Terminate;
end;
end;
procedure tform1.ExecuteFile(
const AFilename:
String;
AParameter, ACurrentDir:
String; AWait: Boolean;
AOnWaitProc: TExecuteWaitEvent=nil);
var
si: TStartupInfo;
pi: TProcessInformation;
bTerminate: Boolean;
begin
bTerminate := False;
if Length(ACurrentDir) = 0
then
ACurrentDir := ExtractFilePath(AFilename);
if AnsiLastChar(ACurrentDir) = '
\'
then
Delete(ACurrentDir, Length(ACurrentDir), 1);
FillChar(si, SizeOf(si), 0);
with si
do begin
cb := SizeOf(si);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_HIDE;
end;
FillChar(pi, SizeOf(pi), 0);
AParameter := Format('
"%s" %s', [AFilename, TrimRight(AParameter)]);
if CreateProcess(
Nil, PChar(AParameter),
Nil,
Nil, False,
CREATE_DEFAULT_ERROR_MODE
or CREATE_NEW_CONSOLE
or
NORMAL_PRIORITY_CLASS,
Nil, PChar(ACurrentDir), si, pi)
then
try
if AWait
then
while WaitForSingleObject(pi.hProcess, 50) <> Wait_Object_0
do
begin
if Assigned(AOnWaitProc)
then
begin
AOnWaitProc(pi, bTerminate);
if bTerminate
then
TerminateProcess(pi.hProcess, Cardinal(-1));
end;
Application.ProcessMessages;
end;
finally
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
end;
end.