delphi - 如何检查是否有正在使用给定句柄运行的进程

标签 delphi executable handle auto-update

场景: 从 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/

相关文章:

delphi - 在 Delphi xe2 中初始化给定类的变量

image - 如何在delphi中完全删除面板边框?

windows - 有没有办法为 Mac/Windows 制作双重可执行文件?

function - 在 Matlab 中通过函数句柄传递附加参数

Delphi indy 将流发送到客户端

delphi - 找到托盘图标

嵌入网页的c exec

iphone - 我想知道IPA文件的执行流程

Delphi:检查文件是否正在使用

c# - 在运行时用另一个窗口控件替换窗口控件