Tôi đã sử dụng chức năng dưới đây để bắt đầu và chờ cho đến khi kết thúc quá trình.Delphi 7 32 bit thực hiện và đợi 64 bit quá trình
Nó hoạt động tốt để bắt đầu và chờ xử lý 32 bit trên hệ điều hành 32 bit hoặc 64 bit.
Nhưng trên hệ điều hành 64 bit, nó trả về ngay lập tức khi tôi khởi chạy quá trình 64 bit (WaitForSingleObject = WAIT_OBJECT_0). Ví dụ, nếu ứng dụng của tôi (32 bit), khởi động mstsc.exe trên một hệ điều hành 32 bit nó là ok nhưng nó không chờ đợi trên một hệ điều hành 64 bit chắc chắn bởi vì mstsc.exe là một chương trình 64 bit.
Bất kỳ giải pháp nào?
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;
Một bên. Ở đây bạn đang bắt đầu một quá trình mới, nơi bạn biết thực thi. CreateProcess là API cho điều đó. ShellExecuteEx là khi bạn cần trình bao để tìm ra cách thực hiện nó.Vì bạn biết tên của tệp thực thi, điều này có ý nghĩa hơn, theo quan điểm của tôi, để gọi trực tiếp đến CreateProcess. –
@DavidHeffernan Bạn nói đúng! – NMD