Gibt es eine Möglichkeit, ShellExecute anzuweisen, so lange zu warten, bis das externe Programm beendet ist?
Das geht folgendermaßen:
function ExecuteFileAndWait(FileName, Params, DefaultDir: string; ShowCmd: DWord):boolean;
var
err:smiley:Word;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
zAppName,
zCurDir : array [0..MAX\_PATH] of char;
begin
{ SW\_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW\_SHOWMINIMIZED Activates the window and displays it as a minimized window.
SW\_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.
SW\_SHOWNA Displays the window in its current state. The active window remains active.
SW\_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW\_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and
position. An application should specify this flag when displaying the window for the first time.
}
result:=false;
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF\_USESHOWWINDOW;
StartupInfo.wShowWindow := ShowCMD;
StrPCopy(zAppname,filename+' '+params);
if defaultDir='' then
GetDir(0,DefaultDir);
StrPCopy(zCurDir,DefaultDir);
if not CreateProcess(
nil, { pointer to executable}
zAppName, { 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 }
zCurDir, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS\_INF }
begin
showlasterror;
end
else
begin
// Warten, bis der Process beendet wurde
repeat
err:=WaitforSingleObject(ProcessInfo.hProcess,100); // 100 MSec Warten
application.ProcessMessages;
until (errWAIT\_TIMEOUT);
if errWAIT\_FAILED then
result:=true
else
begin
showmessage(GetLastErrorMsg);
end;
end;
end;
Viele Grüße Joachim