function ExecAndWait(Filename, Params: Widestring;
WindowState: word = SW_SHOWNORMAL): boolean;
var
ShExecInfoW: SHELLEXECUTEINFOW;
ShExecInfoA: SHELLEXECUTEINFOA;
r : Cardinal;
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecuteex.asp
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/structures/shellexecuteinfo.asp
begin
Result := false;
if Filename = '
'
then exit;
if not WideFileExists(FileName)
then
begin
// Log (true,P_ERROR,WideFormat(_('Kann die angegebene Datei nicht finden: %s'),[FileName]));
exit;
end;
if Win32IsUnicode
then
begin
ShExecInfoW.Wnd := GetForegroundWindow;
ShExecInfoW.cbSize := sizeof(SHELLEXECUTEINFOW);
ShExecInfoW.fMask := SEE_MASK_NOCLOSEPROCESS;
ShExecInfoW.lpVerb := '
open';
ShExecInfoW.lpFile := PWideChar(Filename);
ShExecInfoW.lpParameters := PWideChar(Params);
ShExecInfoW.lpDirectory := PWideChar(WideExtractFileDir(Filename));
ShExecInfoW.nShow := WindowState;
Result := ShellExecuteExW(@ShExecInfoW);
end
else
begin
ShExecInfoA.Wnd := GetForegroundWindow;
ShExecInfoA.cbSize := sizeof(SHELLEXECUTEINFOA);
ShExecInfoA.fMask := SEE_MASK_NOCLOSEPROCESS;
ShExecInfoA.lpVerb := '
open';
ShExecInfoA.lpFile := PChar(AnsiString(Filename));
ShExecInfoA.lpParameters := PChar(AnsiString(Params));
ShExecInfoA.lpDirectory := PChar(AnsiString(WideExtractFileDir(Filename)));
ShExecInfoA.nShow := WindowState;
Result := ShellExecuteExA(@ShExecInfoA);
end;
try
if Result
then
begin
if Win32IsUnicode
then
r := WaitForSingleObject(ShExecInfoW.hProcess, INFINITE)
else
r := WaitForSingleObject(ShExecInfoA.hProcess, INFINITE);
end;
finally
if Win32IsUnicode
then
CloseHandle(ShExecInfoW.hProcess)
else
CloseHandle(ShExecInfoA.hProcess);
end;
end;