gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
C# findwindow and get process id
- User
C# findwindow and get process id
- Assistant
To use the
FindWindowfunction in C# and get the process ID, you can follow these steps:- Import the necessary libraries:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices;- Define the
FindWindowandGetWindowThreadProcessIdfunctions from the user32.dll library:
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = false)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);- Use the
FindWindowfunction to get the handle of the window you want to identify:
string windowClassName = null; string windowTitle = "YourWindowTitle"; IntPtr hWnd = FindWindow(windowClassName, windowTitle); if (hWnd != IntPtr.Zero) { uint processId; GetWindowThreadProcessId(hWnd, out processId); Process process = Process.GetProcessById((int)processId); Console.WriteLine("Window Found! Process ID: " + processId); Console.WriteLine("Process Name: " + process.ProcessName); } else { Console.WriteLine("Window not found!"); }