TerminateProcess 在 Windows 7 中不起作用

发布于 2024-10-10 18:53:39 字数 455 浏览 4 评论 0原文

我正在win7平台上用c++编写一个应用程序,需要关闭另一个应用程序。 我使用的步骤是:

  1. 使用 EnumProcess() 枚举所有进程。
  2. 使用 OpenProcess() 打开进程句柄。访问权限为PROCESS_ALL_ACCESS|PROCESS_VM_READ。
  3. 然后使用 EnumProcessModules() 枚举进程模块,
  4. 我使用 GetModuleBaseName() 提取模块名称,并将其与我拥有的进程名称进行比较。
  5. 当我找到匹配项时,我使用 TerminateProcess() 来终止该进程。

我面临的问题是这在 WindowsXP 中有效,但在 Windows 7(64 位)中无效。使用 getlasterror(), 我收到错误“访问被拒绝”。我猜这与访问权限有关。 有什么办法可以在两个平台上做到这一点吗?或者有win7专用的API吗?

I am writing an application in c++ on win7 platform which needs to close another application.
The steps I use are:

  1. Enumerate all processes with EnumProcess().
  2. Open a Process handle with OpenProcess(). The access rights are PROCESS_ALL_ACCESS|PROCESS_VM_READ.
  3. Then enumerate process modules with EnumProcessModules()
  4. I extract the module name with GetModuleBaseName() and compare it with the process name that I have.
  5. When I find a match, I use TerminateProcess() to kill the process.

The problem I am facing is this works in WindowsXP but not in Windows 7(64 bit). Using getlasterror(),
I get the error as "Access Denied". I guess it has something to do with access rights.
Is there any way I can do this on both the platforms? Or is there an API specific to win7?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莳間冲淡了誓言ζ 2024-10-17 18:53:39

我也有同样的问题。寻找答案很久了,终于找到了。

当你想终止另一个程序时,你需要一个句柄。句柄需要权限才能与其他进程一起使用。终止进程需要名为 PROCESS_TERMINATE 的特定权限。打开手柄进行终止时使用它,它可能会起作用。在 Windows 7 上,它对我有用。

总而言之,这里是正确使用 TerminateProcess 所需的代码。小心处理;)

Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessID As Long) As Long
Declare Function TerminateProcess Lib "kernel32.dll" ( _
    ByVal ApphProcess As Long, _
    ByVal uExitCode As Long) As Long

Const PROCESS_TERMINATE = &H1

Private Sub KillProcess(ByVal ProcessID As Long)
    Dim pHandle As Long
    pHandle = OpenProcess(PROCESS_TERMINATE, 0, ProcessID)
    Call TerminateProcess(pHandle, 0)
End Sub

I had the same problem. Been looking very long for an answer and finally found it.

When you want to terminate another program you need a handle. A handle needs permissions to work with the other process. Terminating the process needs a specific permissions called PROCESS_TERMINATE. Use that when opening a handle for termination and it will probably work. It did for me, on Windows 7.

To sum things up, here the code you need to correctly use TerminateProcess. Handle with care ;)

Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessID As Long) As Long
Declare Function TerminateProcess Lib "kernel32.dll" ( _
    ByVal ApphProcess As Long, _
    ByVal uExitCode As Long) As Long

Const PROCESS_TERMINATE = &H1

Private Sub KillProcess(ByVal ProcessID As Long)
    Dim pHandle As Long
    pHandle = OpenProcess(PROCESS_TERMINATE, 0, ProcessID)
    Call TerminateProcess(pHandle, 0)
End Sub
神经暖 2024-10-17 18:53:39

您是否以管理员权限运行程序,并且是否终止同一用户的处理?

Are you running your program with Administrator privileges, and are you terminating processed of the same user?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文