public static Process GetProcess(IntPtr hwnd) { try { uint pid = 0; GetWindowThreadProcessId (hwnd, out pid); return Process.GetProcessById ((int)pid); //Gets the process by ID. } catch (Exception) { return null; } }