场景: 从 App1 中,我需要传递 App1.Handle 作为参数来执行 App2。 App2 应该等到 App1 关闭。此后,App2 应使用更新版本替换 App1.exe 文件。
- 是否有更好的方法来更新正在运行的可执行文件?
- 如果没有..在App2中,我知道App1.Handle,我应该检查一下 如果 App1 已关闭。如何验证这一点 App1.Handle?
编辑:
应用程序1:
var
ProcessHandle : THandle;
begin
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, GetCurrentProcessId());
//Is PROCESS_ALL_ACCESS needed?
ShellExecute(0, 'open', 'App2.exe', PChar(IntToStr(ProcessHandle)), '.\', SW_SHOW);
end;
应用程序2:
var
SenderHandle : THandle;
begin
if(ParamStr(1) <> '') then
begin
SenderHandle := StrToInt(ParamStr(1));
WaitForSingleObject(SenderHandle, INFINITE);
ShowMessage('App1 Terminated!');
//Showmessage is executed when App1 is still running, what's wrong?
end;
end;
最佳答案
App1.Handle
表示一个窗口句柄。 App2 需要等待 App1 的进程句柄。要获取 App1 的进程句柄,请使用 OpenProcess()
和 GetCurrentProcessId()
作为进程 ID,或使用 DuplicateHandle()
和 GetCurrentProcess( )
作为源句柄。然后,您可以将句柄传递给 App2,并让 App2 使用 WaitForSingleObject()
等待它。当 App1 退出时,句柄将收到信号。然后App2就可以关闭句柄并替换App1.exe。
关于delphi - 如何检查是否有正在使用给定句柄运行的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141600/