uses
ShellAPI;
procedure ExecAndWait(Filename, Params:
string; WindowState: word = SW_SHOWNORMAL);
var
ShExecInfo: SHELLEXECUTEINFO;
RetValue: DWORD;
const
SEE_MASK_NOASYNC= $100;
begin
ZeroMemory(@ShExecInfo, SizeOf(ShExecInfo));
ShExecInfo.Wnd := Application.Handle ;
//GetForegroundWindow;
ShExecInfo.cbSize := sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS
or SEE_MASK_NOASYNC;
ShExecInfo.lpVerb := '
open';
ShExecInfo.lpFile := PChar(Filename);
ShExecInfo.lpParameters := PChar(Params);
ShExecInfo.lpDirectory := PChar(ExtractFileDir(Filename));
ShExecInfo.nShow := WindowState;
if ShellExecuteEx(@ShExecInfo)
then
begin
repeat
RetValue := MsgWaitForMultipleObjects(1, ShExecInfo.hProcess, False, INFINITE, QS_ALLINPUT);
if RetValue <> WAIT_OBJECT_0
then
Application.ProcessMessages;
until RetValue = WAIT_OBJECT_0;
CloseHandle(ShExecInfo.hProcess);
end
else
RaiseLastOSError;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ExecAndWait('
C:\Windows\Notepad.exe', '
');
ShowMessage('
Fertig');
except
on E:
Exception do
ShowMessage(E.
Message);
end;
end;