How to enumerate all windows belonging to a particular process using .NET?
Use the Win32 API EnumWindows (if you want child windows EnumChildWindows)), or alternatively you can use EnumThreadWindows .
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
Then check which process each window belongs to by using the Win32 API GetWindowThreadProcessId
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
How to enumerate all windows within a process?
3rd party aplication launched other windows not as child windows.
It is possible to find out what is structure using Spy++ tool which comes with Visual Studio.
After this, I was able to find necessary window using FindWindowEx function using WindowClassName (taken from Spy++):
lastWindows = FindWindowEx(IntPtr.Zero, lastWindows, m.WindowClassName, null);
Get all window handles for a process
Pass IntPtr.Zero
as hWnd
to get every root window handle in the system.
You can then check the windows' owner process by calling GetWindowThreadProcessId
.
Get handles to all windows of a process
The EnumChildWindows function might help you out. The child windows could also have children and so on.
There is also GetWindow and EnumThreadWindows
Enumerate all windows which are always on top
Here's a working example which finds all processes which have a topmost window. Be careful though: Windows Explorer always has a topmost window and you probably don't want to kill that process.
class Program
{
const int WS_EX_TOPMOST = 0x00000008;
const int WS_VISIBLE = 0x10000000;
const int GWL_STYLE = -16;
const int GWL_EXSTYLE = -20;
static void Main(string[] args)
{
var topmostWindowHandles = new ArrayList();
EnumWindows(EnumWindowsCallback, topmostWindowHandles);
var processesToKill = new HashSet<uint>();
foreach (IntPtr hWnd in topmostWindowHandles)
{
uint processId = 0;
GetWindowThreadProcessId(hWnd, out processId);
processesToKill.Add(processId);
}
foreach (uint pid in processesToKill)
{
Process proc = Process.GetProcessById((int)pid);
Console.WriteLine("Killing " + proc.ProcessName);
// kill process, except explorer.exe
}
}
static bool EnumWindowsCallback(IntPtr hWnd, ArrayList lParam)
{
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
int style = GetWindowLong(hWnd, GWL_STYLE);
if ((exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST
&& (style & WS_VISIBLE) == WS_VISIBLE)
{
lParam.Add(hWnd);
}
return true;
}
public delegate bool EnumWindowsProc(IntPtr hwnd, ArrayList lParam);
[DllImport("user32.dll")]
[return:MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ArrayList lParam);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}
How can I list all processes running in Windows?
Finding all of the processes
You can do this through the Process class
using System.Diagnostics;
...
var allProcesses = Process.GetProcesses();
Running Diagnostics
Can you give us some more information here? It's not clear what you want to do.
The Process class provides a bit of information though that might help you out. It is possible to query this class for
- All threads
- Main Window Handle
- All loaded modules
- Various diagnostic information about Memory (Paged, Virtual, Working Set, etc ...)
- Basic Process Information (id, name, disk location)
EDIT
OP mentioned they want to get memory and CPU information. These properties are readily available on the Process class (returned by GetProcesses()). Below is the MSDN page that lists all of the supported properties. There are various memory and CPU ones available that will suite your needs.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
Code:
Add this line to your using list:
using System.Diagnostics;
Now you can get a list of the processes with the Process.GetProcesses() method, as seen in this example:
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
How to enumerate all windows belonging to a particular process using .NET?
Use the Win32 API EnumWindows (if you want child windows EnumChildWindows)), or alternatively you can use EnumThreadWindows .
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
Then check which process each window belongs to by using the Win32 API GetWindowThreadProcessId
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
Win32: How do I enumerate all the threads belonging to a process in C++?
See Thread Walking and Enumerating threads in Windows
Related Topics
How to Do Pagination in Datagridview in Winform
Pass Table Valued Parameter Using Ado.Net
Alphanumeric Sorting Using Linq
Process.Waitforexit() Asynchronously
How to Convert JSON Text into Objects Using C#
Static VS Non-Static Class Members
How to Ignore Unknown Enum Values During JSON Deserialization
How to "Steal" an Event Handler from One Control and Give It to Another
JSON.Net Disable the Deserialization on Datetime
How to Call Functions in a Main View Model from Other View Models
How to Find All Partitions of a Set
Why Can't C# Infer Type from This Seemingly Simple, Obvious Case
How to Read Data of an Excel File Using C#
What Is "Best Practice" for Comparing Two Instances of a Reference Type
How to Automatically Filter Out Soft Deleted Entities with Entity Framework