如何防止远程进程在 Windows 中被关闭?

How to prevent a remote process from being closed in Windows?

有一个程序我不能修改它的代码已经打开。我想创建一个 "watcher" 来挂钩该程序的关闭事件,以便隐藏程序的主要 window 而不是在用户尝试关闭该程序时被关闭。

我用谷歌搜索了一下,有人说挂钩名为 OpenProcess 的 API 可以。我尝试使用名为 "EasyHook" 的 C# 库并成功注入远程进程并挂钩 MessageBeep API。然后我尝试hook OpenProcessTerminateProcess,但是这两个方法的hook方法一直没有被调用。

那么hook的正确方法是什么,或者有没有其他方法可以实现我的目的?如果没有办法阻止进程被任务管理器终止,是否有任何技巧,如挂钩 window 等的关闭按钮?

PS。 Windows的内核编程和驱动程序编程我都不会,所以如果可以的话我想在用户态下实现。

I want to create a "watcher" to hook the closing event of that program in order to make the main window of the program hidden rather than being closed when users try to close that program.

没有您可以挂接的进程关闭事件。但是,对于 GUI 程序,有 WM_CLOSE and WM_SYSCOMMAND|SC_CLOSE window messages that you can intercept with SetWindowsHookEx().

I have googled it and someone says hook the API named OpenProcess would work.

谁说挂钩 OpenProcess() 是解决此问题的方法,要么是错误的,要么是您误读了该挂钩的实际用途。

I tried using a C# lib called "EasyHook" and succesfully inject a remote process and hook MessageBeep API. Then I tried hooking OpenProcess and TerminateProcess, but the hooked methods of those two methods were never called.

当然,因为您试图在正在终止的进程中挂钩它们,但这不是调用它们的地方,它们是在正在终止的进程中调用的(即在任务管理器本身中) ).

If there is no way to prevent the process being terminated with task manager

如果使用暴力破解"End Process (Tree)"选项,那么就没有catch/block选项了。 "Application" 选项卡上的 "End Task" 选项尝试在诉诸暴力之前使用 window 消息执行正常终止。

is there any tricks like hooking the close button of the window etc.?

请参阅我上面的第一条评论。