通过PID获取过程句柄 [英] get process handle by PID

查看:794
本文介绍了通过PID获取过程句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过进程名称获取进程的句柄。

I want to get the handle of a process by the process name.

我有PID,但是当我使用 openProcess 获取句柄总是返回0或180,我用来使PID正常工作的功能

I have PID but when I use openProcess to get the handle always it will return 0 or 180, the function that I use to get the PID working properly

句柄:= OpenProcess(PROCESS_QUERY_INFORMATION或PROCESS_VM_READ,False,PID);

我该怎么办?

推荐答案

除非你使用 CreateProcess ,所有知道的都是它的名字,没有直接的方法来获取进程句柄。

There is no direct way to get a process handle when all you know is its name, unless you're using CreateProcess.

相反,您可以使用 CreateToolhelp32Snapshot Process32First Process32Next 搜索全部具有您想要的名称的进程。请记住,可能有多个进程具有相同的名称。那些功能会告诉你进程ID。一旦你这样做,你可以使用 OpenProcess ,如你已经演示的。如果 OpenProcess 返回零以外的值(例如180),那么它给你一个有效的进程句柄。

Instead, you can use CreateToolhelp32Snapshot, Process32First, and Process32Next to search for all processes having the name you want. Keep in mind that there may be multiple processes with the same name. Those functions will tell you the process ID. Once you have that, you can use OpenProcess, as you've already demonstrated. If OpenProcess returns something other than zero (such as 180), then it has given you a valid process handle.

这篇关于通过PID获取过程句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆