简体   繁体   中英

Why isn't FindWindowEx finding the label in my window?

I'm trying to debug a little test driver application written in C#. It opens up several instances of a test app we have. The test app connects to a server and when successfully connected, displays "REMOTE_CONNECTED" in a label. So the test driver is looking for that before attempting to feed in data to the test app.

Here's what the code looks like:

  Console.Out.WriteLine("MAIN HANDLE IN GETCONN: " + Hwnd);
  //Attempt to find if we have have connected to the remote server
  IntPtr connHwnd = FindWindowEx(Hwnd, IntPtr.Zero, null, "REMOTE_CONNECTED");

That connHwnd always comes back a IntPtr.Zero and the Hwnd printed to the console is the handle I expect of the test app. The test driver sits in a loop for a while, repeatedly calling the above code until it finds that label. I can see on the screen that the label is displaying "REMOTE_CONNECTED" yet the function isn't finding it.

Furthermore, other folks have gotten this to work on XP (whereas I'm on Windows 7).

Finally, if I run this in Visual Studio and set some breakpoints, then it seems to work. So it doesn't seem like a logic flaw, but some sort of timing or contention issue.

My issue turned out to be similar to that in this question:

Why can't get the main window handle for a started process?

My MainWindowHandle was not set to the window I expected it to be (which I figured out using Spy++, thanks Rob P.!). So I wrote some code using EnumWindows to find the Window I was looking for (see Joshua's answer in the linked post) and then used that Window Handle to pass into the FindWindowEx and everything worked as expected.

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