tags:

views:

381

answers:

1

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

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

Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);

What should I do?

+7  A: 

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

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.

Rob Kennedy
U means the process handle is not unique ? so 2 process that are runinng may have two PID and one handle?
sam
I don't know how you reached that conclusion from what I wrote in my answer. Anyway, a process may have many handles open for it. You could call OpenProcess twice on the same PID, and you should get two different numeric values — two handles, one process. You could call OpenProcess on the same PID from two different programs, and you might get two different values, or you might get the same value in each program, but those are still two different handles.
Rob Kennedy
ok THX MR Kennedy
sam