function RunAndWait(Handle: THandle; Ejecutable, Argumentos: string; const RunDirectory: PChar = nil; const Visibilidad: integer = SW_SHOWNORMAL; MensajeSiCorrecto: boolean = True): DWORD;
var
Info : TShellExecuteInfo;
pInfo : PShellExecuteInfo;
ExitCode : word;
P : PChar;
begin
pInfo := @Info;
with Info do
begin
cbSize := SizeOf(Info);
fMask := SEE_MASK_NOCLOSEPROCESS;
wnd := Handle;
lpVerb := nil;
lpFile := PChar(Ejecutable);
lpParameters := PChar(Argumentos + #0);
if RunDirectory = '' then
lpDirectory := nil
else
lpDirectory := PChar(RunDirectory + #0);
nShow := Visibilidad;
hInstApp := 0;
end;
if not ShellExecuteEx(pInfo) then
begin
Result := GetLastError;
if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
nil,
Result,
0, @P,
0,
nil) <> 0 then
begin
ShowMessage(P);
LocalFree(integer(P));
end;
end
else
Result := 0;
repeat
ExitCode := WaitForSingleObject(Info.hProcess, 500);
Application.ProcessMessages;
until (ExitCode <> WAIT_TIMEOUT);
ExitCode := GetLastError;
GetExitCodeProcess(Info.hProcess, Result);
if ((Result < 32) and (ExitCode = 0)) then
begin
ExitCode := GetLastError;
if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
nil,
ExitCode,
0, @P,
0,
nil) <> 0 then
begin
if MensajeSiCorrecto or (ExitCode <> 0) then
ShowMessage(P);
LocalFree(integer(P));
end;
end
else
Result := 0; end;