Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if window is a MessageBox

Tags:

c#

interop

winapi

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);

[DllImport("user32.dll")]
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;
        listWindows.Add(hwnd);
        return true;
    }

    internal static IEnumerable<IntPtr> GetAllWindows()
    {
        listWindows.Clear();
        NativeMethods.EnumWindows(IsWindowOrDialog, IntPtr.Zero);
        return listWindows;
    }
}
like image 912
Tomtom Avatar asked Nov 12 '22 11:11

Tomtom


1 Answers

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);
like image 68
Chibueze Opata Avatar answered Nov 15 '22 04:11

Chibueze Opata