1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| function ExecAndWait(Filename, Params: String;
WindowState: Word = SW_SHOWNORMAL): boolean;
var
{$IFDEF UNICODE} ShExecInfoW: SHELLEXECUTEINFOW; {$ENDIF}
ShExecInfoA: SHELLEXECUTEINFOA;
// MSDN: ShellExecuteEx, ShellExecuteInfo
begin
Result := false;
if (Filename = '') or not FileExists(FileName) then
exit;
{$IFDEF UNICODE}
if Win32IsUnicode then
begin
ShExecInfoW.Wnd := GetForegroundWindow;
ShExecInfoW.cbSize := SizeOf(SHELLEXECUTEINFOW);
ShExecInfoW.fMask := SEE_MASK_NOCLOSEPROCESS;
ShExecInfoW.lpVerb := 'open';
ShExecInfoW.lpFile := PWideChar(WideString(Filename));
ShExecInfoW.lpParameters := PWideChar(WideString(Params));
ShExecInfoW.lpDirectory := PWideChar(WideString(ExtractFileDir(Filename)));
ShExecInfoW.nShow := WindowState;
Result := ShellExecuteExW(@ShExecInfoW);
try
if Result then WaitForSingleObject(ShExecInfoW.hProcess, INFINITE);
finally
CloseHandle(ShExecInfoW.hProcess);
end;
end
else
{$ENDIF}
begin
ShExecInfoA.Wnd := GetForegroundWindow;
ShExecInfoA.cbSize := sizeof(SHELLEXECUTEINFOA);
ShExecInfoA.fMask := SEE_MASK_NOCLOSEPROCESS;
ShExecInfoA.lpVerb := 'open';
ShExecInfoA.lpFile := PAnsiChar(AnsiString(Filename));
ShExecInfoA.lpParameters := PAnsiChar(AnsiString(Params));
ShExecInfoA.lpDirectory := PAnsiChar(AnsiString(ExtractFileDir(Filename)));
ShExecInfoA.nShow := WindowState;
Result := ShellExecuteExA(@ShExecInfoA);
try
if Result then WaitForSingleObject(ShExecInfoA.hProcess, INFINITE);
finally
CloseHandle(ShExecInfoA.hProcess);
end;
end;
end; |
Partager