简体   繁体   中英

FindWindowEx returns 0 when used on specific application

I am trying to find all child controls of a given window. I can get the window's handle, which I have verified using Inspect.exe (from the Windows Development Kit). The problem is when I call FindWindowEx the function returns 0 ( IntPtr.Zero to be precise) while I can find the controls with Inspect.exe .

Here is my code

[DllImport("user32.dll", SetLastError=true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, 
          IntPtr childAfter, string className, string windowTitle);

public static List<IntPtr> EnumChildren(IntPtr hwnd)
{
    IntPtr zero = IntPtr.Zero;
    List<IntPtr> list = new List<IntPtr>();
    do
    {
        zero = FindWindowEx(hwnd, zero, null, null); // Returns 0
        if (zero != IntPtr.Zero)
        {
            list.Add(zero);
        }
    }
    while (zero != IntPtr.Zero);
    return list;
}

I have tried using the following, which all return 0 as well:

zero = FindWindowEx(hwnd, zero, "TextBox", null);
zero = FindWindowEx(hwnd, zero, "TextBox", "Text");
zero = FindWindowEx(hwnd, zero, String.Empty, String.Empty);
zero = FindWindowEx(hwnd, zero, "TextBox", String.Empty);

I know there is a way to find the window's controls as Inspect.exe is doing it. I have tried using EnumChildWindows but get the same result, eg an empty list. Note that with other software (I have tried Thunderbird and KeePass so far) the FindWindowEx function works properly, just not with the application I have to work with.

I have tested using EnumChildWindows to make sure there is only one window with the title I am looking for and it is the only one. I really can't explain why I can't get any of its controls.

What am I doing wrong and is there another way to get all child windows for a given window?

You are calling:

zero = FindWindowEx(hwnd, zero, null, null);

And this returns 0. Since you pass NULL for both class name and window name, FindWindowEx considers all children of hwnd . Since you pass NULL for hwndChildAfter , the documentation tells you that:

If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.

In other words, the only conclusions that can be drawn are that either:

  1. The hwnd parameter is invalid, or
  2. The window specified by hwnd has no child windows.

Now, for point 1, let's assume that you are capable of supplying a valid window handle. In which case the only remaining possibility is that hwnd has no children. That is quite plausible. Many GUI frameworks use non-windowed controls. That also tallies with the fact that EnumChildWindows returns no windows.

You've used the Inspect tool to look at the application. Let's see what MSDN says about Inspect .

Inspect (Inspect.exe) is a Windows-based tool that enables you select any UI element and view the element's accessibility data. You can view Microsoft UI Automation properties and control patterns, as well as Microsoft Active Accessibility properties. Inspect also enables you to test the navigational structure of the automation elements in the UI Automation tree, and the accessible objects in the Microsoft Active Accessibility hierarchy.

The fundamental problem is that you have chosen the wrong tool to solve this problem. Instead of poking around with window hierarchies, you should use the automation API to perform this task. That is how Inspect is able to decompose the controls of this application and will have to do so also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM