function ExecAndWait(Filename, Params:
String;
WindowState: Word = SW_SHOWNORMAL; Wait : Boolean = False): Cardinal;
var
{$IFDEF UNICODE} ShExecInfoW: SHELLEXECUTEINFOW;
{$ENDIF}
ShExecInfoA: SHELLEXECUTEINFOA;
// MSDN: ShellExecuteEx, ShellExecuteInfo
begin
Result := 0;
if (Filename = '
')
or not FileExists(FileName)
then
exit;
{$IFDEF UNICODE}
if Win32IsUnicode
then
begin
ProgramStarting := True;
ZeroMemory(@ShExecInfoW, SizeOf(ShExecInfoW));
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;
try
ShellAPI.ShellExecuteExW(@ShExecInfoW);
Result := ShExecInfoW.hProcess;
if Wait
then WaitForSingleObject(ShExecInfoW.hProcess, INFINITE);
finally
CloseHandle(ShExecInfoW.hProcess);
ProgramStarting := False;
end;
end
else
{$ENDIF}
begin
ZeroMemory(@ShExecInfoA, SizeOf(ShExecInfoA));
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;
try
ShellExecuteExA(@ShExecInfoA);
Result := ShExecInfoA.hProcess;
if Wait
then WaitForSingleObject(ShExecInfoA.hProcess, INFINITE);
finally
CloseHandle(ShExecInfoA.hProcess);
end;
end;
end;