unit ShellApiEx;
interface
uses
Windows, ShellApi;
{.$DEFINE UNICODE}
function ShellExecuteAndWaitA(hWnd: HWND;
Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer;
bWait: BOOL): HINST;
stdcall;
function ShellExecuteAndWaitW(hWnd: HWND;
Operation, FileName, Parameters, Directory: PWideChar; ShowCmd: Integer;
bWait: BOOL): HINST;
stdcall;
type
TShellExecuteAndWait =
function(hWnd: Windows.HWND;
{$IFDEF UNICODE}
Operation, FileName, Parameters, Directory: PWideChar;
{$ELSE}
Operation, FileName, Parameters, Directory: PAnsiChar;
{$ENDIF}
ShowCmd: Integer; bWait: BOOL): HINST;
stdcall;
var
ShellExecuteAndWait: TShellExecuteAndWait =
{$IFDEF UNICODE}
ShellExecuteAndWaitW;
{$ELSE}
ShellExecuteAndWaitA;
{$ENDIF}
implementation
function ShellExecuteAndWaitW(hWnd: HWND;
Operation, FileName, Parameters, Directory: PWideChar; ShowCmd: Integer;
bWait: BOOL): HINST;
stdcall;
var
sei: TShellExecuteInfoW;
begin
ZeroMemory(@sei, sizeof(sei));
with sei
do
begin
cbSize := sizeof(sei);
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := hWnd;
lpVerb := Operation;
lpFile := FileName;
lpParameters := Parameters;
lpDirectory := Directory;
nShow := ShowCmd;
end;
if ShellExecuteExW(@sei)
then
try
if bWait
then
WaitForSingleObject(sei.hProcess, INFINITE);
finally
CloseHandle(sei.hProcess);
end;
result := sei.hInstApp;
end;
function ShellExecuteAndWaitA(hWnd: HWND;
Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer;
bWait: BOOL): HINST;
stdcall;
var
sei: TShellExecuteInfoA;
begin
ZeroMemory(@sei, sizeof(sei));
with sei
do
begin
cbSize := sizeof(sei);
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := hWnd;
lpVerb := Operation;
lpFile := FileName;
lpParameters := Parameters;
lpDirectory := Directory;
nShow := ShowCmd;
end;
if ShellExecuteExA(@sei)
then
try
if bWait
then
WaitForSingleObject(sei.hProcess, INFINITE);
finally
CloseHandle(sei.hProcess);
end;
result := sei.hInstApp;
end;
end.