1. Home
  2. How to get process id and thread id from a Window Handle in .NET CF?

How to get process id and thread id from a Window Handle in .NET CF?

KB ID: 42G2012285
Views: 161
Updated: May 2021

Specify the namespace for doing P/Invoke stuff i.e. calling Win32 API functions from managed code.

using System.Runtime.InteropServices;

GetWindowThreadProcessId Win32 function retrieves the identifiers of the process and thread that created the specified window.

Here is how we declare GetWindowThreadProcessId for use in managed code (c#).


[DllImport("coredll.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

Description:

  • hWnd is the window handle
  • lpdwProcessId stores the process identifier after the method returns
  • return value of the function is the id of the thread that created the window

Calling GetWindowThreadProcessId via P/Invoke:

// Set the hWnd value below with window handle of your interest
IntPtr hWnd = this.Handle;
uint processid = 0;
uint threadid = GetWindowThreadProcessId((IntPtr)hWnd, out processid);

And there you go….

For more details on our products, click here 
If you need further assistance, please submit a ticket here