Tomtom Tomtom - 7 months ago 73
C# Question

Check if window is a MessageBox

I have a function where i can react on appearing windows. Now i want to know if the appearing window is a Messagebox. And if it is one, i want to read the text of it.

I'm already able to extract the Window-Title, Class-Name and Process-Id by

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

But how can i find out the text of a messagebox?

To get all windows i am using this:

internal static class WindowFinder
private static readonly List<IntPtr> listWindows = new List<IntPtr>();

private static bool IsWindowOrDialog(IntPtr hwnd, int lParam)
if (NativeMethods.IsHungAppWindow(hwnd) || !NativeMethods.IsWindowVisible(hwnd))
return true;
return true;

internal static IEnumerable<IntPtr> GetAllWindows()
NativeMethods.EnumWindows(IsWindowOrDialog, IntPtr.Zero);
return listWindows;


I don't know what you're trying exactly but you can:

  1. FindWindow
  2. GetDlgItem and extract the text using
  3. GetWindowText

Since MessageBoxes are of type static (0xFFFF), you should use GetDlgItem and GetWindowText like this:

IntPtr dlgHandle = GetDlgItem(MboxHandle, 0xFFFF);
GetWindowText(dlgHandle, yourStringBuilder, maxTextCount);