function TFormMain.ExecuteFile(FileName : String; Params : String) : Integer; var
err : DWord;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
begin
FillChar(ProcessInfo,SizeOf(TProcessInformation),0);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOWNOACTIVATE;
if CreateProcess(
PChar(filename), { pointer to executable}
PChar(params), { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, {
handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
PChar(ExtractFilePath(ParamStr(0))), { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS_INF }
begin
// Warten, bis der Process beendet wurde
repeat
err:=WaitforSingleObject(ProcessInfo.hProcess,100); // 100 MSec Warten
// Application aktualisieren
Application.ProcessMessages;
until (err<>WAIT_TIMEOUT);
end;
if GetLastError <> 0 then
result := GetLastError;
if GetLastError = 0 then
GetExitCodeProcess(ProcessInfo.hProcess, Cardinal(result));
// Process schließen
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
end;