我曾经使用下面的函数启动并等待unil结束进程.
它适用于在32位或64位OS上启动和等待32位进程.
但是在64位操作系统上,当我启动64位进程(WaitForSingleObject = WAIT_OBJECT_0)时它会立即返回.
例如,如果我的应用程序(32位),在32位操作系统上启动mstsc.exe它没关系,但它不会在64位操作系统上等待,因为mstsc.exe是64位程序.
有解决方案吗
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;
Run Code Online (Sandbox Code Playgroud)
Dav*_*nan 15
我的猜测是发生以下情况:
mstsc.exe.mstsc.exe确定它需要启动它所执行的64位版本mstsc.exe,传递任何命令行参数,然后立即终止.这可以解释为什么您的新流程会立即终止.
一些可能的解决方
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |