uses
ShellAPI;
procedure ExecAndWait(Filename, Params: Widestring; WindowState: word = SW_SHOWNORMAL);
var
ShExecInfo: SHELLEXECUTEINFOW;
const
SEE_MASK_NOASYNC= $100;
begin
ZeroMemory(@ShExecInfo, SizeOf(ShExecInfo));
ShExecInfo.Wnd := GetForegroundWindow;
ShExecInfo.cbSize := sizeof(SHELLEXECUTEINFOW);
ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS
or SEE_MASK_NOASYNC;
ShExecInfo.lpVerb := '
open';
ShExecInfo.lpFile := PWideChar(Filename);
ShExecInfo.lpParameters := PWideChar(Params);
ShExecInfo.lpDirectory := PWideChar(WideString(ExtractFileDir(Filename)));
ShExecInfo.nShow := WindowState;
if ShellExecuteExW(@ShExecInfo)
then
begin
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
CloseHandle(ShExecInfo.hProcess);
end
else
RaiseLastOSError;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ExecAndWait('
C:\Windows\Notepad.exe', '
');
except
on E:
Exception do
ShowMessage(E.
Message);
end;
ShowMessage('
Fertig');
end;