Mutmansky
Mutmansky

Reputation: 587

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.

Upvotes: 2

Views: 1338

Answers (1)

Mutmansky
Mutmansky

Reputation: 587

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.

Upvotes: 1

Related Questions