GetWindowThreadProcessId

[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(
   IntPtr hWnd,
   out uint lpdwProcessId
);

Microsoft documentation: Link

Last updated