Created
February 24, 2011 01:15
-
-
Save meklarian/841560 to your computer and use it in GitHub Desktop.
Enumerates Top-Level Windows, Processes, Thread Ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Source code for an answer corresponding to the following Stack Overflow Question | |
http://stackoverflow.com/questions/5098994 | |
"C# - user32.dll - GetWindowRect problem" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Runtime; | |
using System.Runtime.InteropServices; | |
using System.Diagnostics; | |
namespace EnumPTW | |
{ | |
class Program | |
{ | |
#region Externs and Associated Things | |
[DllImport("user32.dll")] | |
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); | |
[StructLayout(LayoutKind.Sequential)] | |
public struct RECT | |
{ | |
public int Left; // x position of upper-left corner | |
public int Top; // y position of upper-left corner | |
public int Right; // x position of lower-right corner | |
public int Bottom; // y position of lower-right corner | |
} | |
[DllImport("user32.dll", SetLastError = true)] | |
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); | |
// Callback Declaration | |
public delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam); | |
[DllImport("user32.dll")] | |
private static extern int EnumWindows(EnumWindowsCallback callPtr, int lParam); | |
public static bool ReportWindow(IntPtr hwnd, int lParam) | |
{ | |
uint processId = 0; | |
uint threadId = GetWindowThreadProcessId(hwnd, out processId); | |
RECT rt = new RECT(); | |
bool locationLookupSucceeded = GetWindowRect(hwnd, out rt); | |
Console.WriteLine(string.Format("Enumerated Window Handle 0x{0:X8}, Process {1}, Thread {2}", hwnd.ToInt32(), processId, threadId)); | |
if (locationLookupSucceeded) | |
{ | |
Console.WriteLine(string.Format(" Position: {0},{1} through {2},{3}", rt.Left, rt.Top, rt.Right, rt.Bottom)); | |
} | |
else | |
{ | |
Console.WriteLine(" Unable to lookup position through GetWindowRect()"); | |
} | |
try | |
{ | |
Process ownerProcess = Process.GetProcessById((int)processId); | |
bool isMainWindow = ownerProcess.MainWindowHandle.Equals(hwnd); | |
Console.WriteLine(string.Format(" Process: {0}{1}", ownerProcess.ProcessName, isMainWindow ? " (MainWindow)" : " (Other Window)")); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(string.Format(" Unable to lookup Process Information for pid {0}", processId)); | |
Console.WriteLine(string.Format(" - Exception of type {0} occurred.", ex.GetType().Name)); | |
} | |
return true; | |
} | |
#endregion//Externs and Associated Things | |
static void Main(string[] args) | |
{ | |
// Have to declare a delegate so that a thunk is created, so that win32 may call us back. | |
EnumWindowsCallback callBackFn = new EnumWindowsCallback(ReportWindow); | |
EnumWindows(callBackFn, 0); | |
Console.WriteLine("Finished. Press any key to continue."); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment