TerminateProcess 在 Windows 7 中不起作用
我正在win7平台上用c++编写一个应用程序,需要关闭另一个应用程序。 我使用的步骤是:
- 使用 EnumProcess() 枚举所有进程。
- 使用 OpenProcess() 打开进程句柄。访问权限为PROCESS_ALL_ACCESS|PROCESS_VM_READ。
- 然后使用 EnumProcessModules() 枚举进程模块,
- 我使用 GetModuleBaseName() 提取模块名称,并将其与我拥有的进程名称进行比较。
- 当我找到匹配项时,我使用 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:
- Enumerate all processes with EnumProcess().
- Open a Process handle with OpenProcess(). The access rights are PROCESS_ALL_ACCESS|PROCESS_VM_READ.
- Then enumerate process modules with EnumProcessModules()
- I extract the module name with GetModuleBaseName() and compare it with the process name that I have.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的问题。寻找答案很久了,终于找到了。
当你想终止另一个程序时,你需要一个句柄。句柄需要权限才能与其他进程一起使用。终止进程需要名为
PROCESS_TERMINATE
的特定权限。打开手柄进行终止时使用它,它可能会起作用。在 Windows 7 上,它对我有用。总而言之,这里是正确使用
TerminateProcess
所需的代码。小心处理;)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 ;)您是否以管理员权限运行程序,并且是否终止同一用户的处理?
Are you running your program with Administrator privileges, and are you terminating processed of the same user?