如何检查是否有一个使用给定句柄运行的进程

时间:2022-02-25 13:16:50

Scenario: From App1, I need to execute App2 passing App1.Handle as param. App2 should wait until App1's close. After this, App2 should replace App1.exe file with an updated version.

场景:从App1开始,我需要执行App2传递App1.Handle作为参数。 App2应该等到App1关闭。在此之后,App2应该用更新的版本替换App1.exe文件。

  • Are there better ways to update a running executable file?
  • 有更好的方法来更新正在运行的可执行文件吗?

  • If there aren't.. In App2, I know App1.Handle and I should check if App1 has been closed. How can this be verified starting from App1.Handle?
  • 如果没有..在App2中,我知道App1.Handle,我应该检查App1是否已经关闭。如何从App1.Handle开始验证?

EDIT:

App1:

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;

App2:

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;

2 个解决方案

#1


App1.Handle implies a window handle. App2 needs to wait on App1's process handle instead. To get App1's process handle, use OpenProcess() with GetCurrentProcessId() as the process ID, or DuplicateHandle() with GetCurrentProcess() as the source handle. Then you can pass the handle to App2, and have App2 wait on it using WaitForSingleObject(). The handle will be signaled when App1 exits. Then App2 can close the handle and replace App1.exe.

App1.Handle意味着一个窗口句柄。 App2需要等待App1的进程句柄。要获取App1的进程句柄,请使用带有GetCurrentProcessId()的OpenProcess()作为进程ID,或使用带有GetCurrentProcess()的DuplicateHandle()作为源句柄。然后你可以将句柄传递给App2,让App2使用WaitForSingleObject()等待它。当App1退出时,手柄将发出信号。然后App2可以关闭句柄并替换App1.exe。

#2


Why wait for the process to terminate using a PID? Why not just try to overwrite the file? As long as the process is still running, you cannot overwrite the .EXE file.

为什么要等待进程终止使用PID?为什么不尝试覆盖该文件?只要进程仍在运行,就无法覆盖.EXE文件。

Ie. something like:

IE浏览器。就像是:

WHILE NOT CopyFile(NewVersion,InstalledVersion) DO Sleep(100);

Of course you could incorporate a time-out and other safeguards, but the above shows you another way to do it without needing a PID or other value to test for...

当然你可以加入超时和其他安全措施,但上面显示了另一种方法,无需PID或其他值来测试......

#1


App1.Handle implies a window handle. App2 needs to wait on App1's process handle instead. To get App1's process handle, use OpenProcess() with GetCurrentProcessId() as the process ID, or DuplicateHandle() with GetCurrentProcess() as the source handle. Then you can pass the handle to App2, and have App2 wait on it using WaitForSingleObject(). The handle will be signaled when App1 exits. Then App2 can close the handle and replace App1.exe.

App1.Handle意味着一个窗口句柄。 App2需要等待App1的进程句柄。要获取App1的进程句柄,请使用带有GetCurrentProcessId()的OpenProcess()作为进程ID,或使用带有GetCurrentProcess()的DuplicateHandle()作为源句柄。然后你可以将句柄传递给App2,让App2使用WaitForSingleObject()等待它。当App1退出时,手柄将发出信号。然后App2可以关闭句柄并替换App1.exe。

#2


Why wait for the process to terminate using a PID? Why not just try to overwrite the file? As long as the process is still running, you cannot overwrite the .EXE file.

为什么要等待进程终止使用PID?为什么不尝试覆盖该文件?只要进程仍在运行,就无法覆盖.EXE文件。

Ie. something like:

IE浏览器。就像是:

WHILE NOT CopyFile(NewVersion,InstalledVersion) DO Sleep(100);

Of course you could incorporate a time-out and other safeguards, but the above shows you another way to do it without needing a PID or other value to test for...

当然你可以加入超时和其他安全措施,但上面显示了另一种方法,无需PID或其他值来测试......