Skip to main content
added 123 characters in body
Source Link
Thorarin
  • 48.7k
  • 12
  • 77
  • 111

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.

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

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single(p => p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault.

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.

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.

added 463 characters in body; added 63 characters in body; added 89 characters in body
Source Link
Thorarin
  • 48.7k
  • 12
  • 77
  • 111

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

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single(p => p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault.

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.

Looks like you have to loop through them all:

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single(p => p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault.

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

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single(p => p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault.

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.

Source Link
Thorarin
  • 48.7k
  • 12
  • 77
  • 111

Looks like you have to loop through them all:

// IntPtr myHandle = ... Process myProcess = Process.GetProcesses().Single(p => p.Handle == myHandle); 

The above example intentionally fails if the handle isn't found. Otherwise, you could of course use SingleOrDefault.