Ho usato la funzione seguente per avviare e attendere fino alla fine di un processo.Delphi 7 32 bit eseguono e attendono il processo a 64 bit
Funziona correttamente per l'avvio e l'attesa per il processo a 32 bit su un sistema operativo a 32 bit o 64 bit.
Ma su un sistema operativo a 64 bit, viene restituito immediatamente all'avvio di un processo a 64 bit (WaitForSingleObject = WAIT_OBJECT_0).
Ad esempio, se la mia app (32 bit), avvia mstsc.exe su un sistema operativo a 32 bit, è ok ma non attende su un sistema operativo a 64 bit perché mstsc.exe è un programma a 64 bit.
Qualsiasi soluzione?
function gShellExecuteAndWait(
vHandle : HWND;
vOperation : string;
vFichier : string;
vParametres : string;
vRepertoire : string;
vAffichage : Integer;
vDuree : DWORD;
var vErreur : string
) : Boolean;
var
vSEInfo : TShellExecuteInfo;
vAttente : DWORD;
begin
// Initialisation
Result := True;
vErreur := '';
vAttente := 0;
// Initialisation de la structure ShellExecuteInfo
ZeroMemory(@vSEInfo, SizeOf(vSEInfo));
// Remplissage de la structure ShellExecuteInfo
vSEInfo.cbSize := SizeOf(vSEInfo);
vSEInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
vSEInfo.Wnd := vHandle;
vSEInfo.lpVerb := PAnsiChar(vOperation);
vSEInfo.lpFile := PAnsiChar(vFichier);
vSEInfo.lpParameters := PAnsiChar(vParametres);
vSEInfo.lpDirectory := PAnsiChar(vRepertoire);
vSEInfo.nShow := vAffichage;
// L'exécution a réussi
if ShellExecuteEx(@vSEInfo) then
begin
// Attendre la fin du process ou une erreur
while True do
begin
case WaitForSingleObject(vSEInfo.hProcess, 250) of
WAIT_ABANDONED :
begin
Result := False;
vErreur := 'L''attente a été annulée.';
Break;
end;
WAIT_OBJECT_0 :
begin
Break;
end;
WAIT_TIMEOUT :
begin
// Initialisation
vAttente := vAttente + 250;
// Le délai d'attente n'a pas été atteint
if vAttente < vDuree then
begin
Application.ProcessMessages();
end
// Le délai d'attente est dépassé
else
begin
Result := False;
vErreur := 'Le délai d''attente a été dépassé.';
Break;
end;
end;
WAIT_FAILED :
begin
Result := False;
vErreur := SysErrorMessage(GetLastError());
Break;
end;
end;
end;
end
// L'exécution a échoué
else
begin
Result := False;
vErreur := SysErrorMessage(GetLastError());
end;
end;
A parte. Qui stai iniziando un nuovo processo in cui conosci l'eseguibile. CreateProcess è l'API per questo. ShellExecuteEx è quando hai bisogno di shell per capire come farlo.Dal momento che si conosce il nome dell'eseguibile ha più senso, a mio avviso, chiamare direttamente CreateProcess. –
@DavidHeffernan Hai ragione! – NMD