10

can someone tell me how i can capture a running process in c# using the process class if i already know the handle?

Id rather not have not have to enumerate the getrunning processes method either. pInvoke is ok if possible.

5 Answers 5

11

In plain C#, it looks like you have to loop through them all:

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single( p => p.Id != 0 && p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault. Apparently, it doesn't like you requesting the handle of process ID 0, hence the extra condition.

Using the WINAPI, you can use GetProcessId. I couldn't find it on pinvoke.net, but this should do:

[DllImport("kernel32.dll")] static extern int GetProcessId(IntPtr handle); 

(signature uses a DWORD, but process IDs are represented by ints in the .NET BCL)

It seems a bit odd that you'd have a handle, but not a process ID however. Process handles are acquired by calling OpenProcess, which takes a process ID.

Sign up to request clarification or add additional context in comments.

4 Comments

i get an access denied message (win32 exception)?
Fixed, but perhaps you should use the WINAPI instead then. That is what you preferred in the first place :)
process.handle gives access denied but. process.MainWindowHandle runs error free
Bad -1... MainWindowHandle is for something completely different.
3
using System.Diagnostics; class ProcessHandler { public static Process FindProcess( IntPtr yourHandle ) { foreach (Process p in Process.GetProcesses()) { if (p.Handle == yourHandle) { return p; } } return null; } } 

Comments

1

There seems to be no simple way to do this by the .Net API. The question is, where you got that handle from? If by the same way you can get access to the processes ID, you could use:

Process.GetProcessById (int iD)

4 Comments

Getting it by ID would certainly be better/easier, but it's possible by handle. See the other answers.
an API exposes the HWND but not the ID
The question remains, where the handle comes from. If a win32 API is used, maybe another API call should be issued to obtain the process ID from the handle. Another issue is, that handles are only valid within the same process, IDs are system wide
Yeah, I agree that having a handle, but not an ID is very odd.
1

I'm using these methods for long time:

 public static Process FindProcess(IntPtr handle) => FindProcess(p => p.Handle == handle); public static Process FindProcess(int id) => FindProcess(p => p.Id == id); public static Process FindProcess(string title) => FindProcess(p=> p.MainWindowTitle == title); public static Process FindProcess(Func<Process,bool> comparer) { foreach (Process p in Process.GetProcesses()) if (comparer(p)) return p; return null; } 

Enjoy...

Comments

0

You could use the GetWindowThreadProcessId WinAPI call

http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html

To get the Process Id - then get a Process object using that.....

But why don't you want to enumerate the ids of the running processes?

1 Comment

That one takes a window handle. He has a process handle. The WINAPI GetProcessId is what he needs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.