简体   繁体   English

为什么FindWindowEx无法在我的窗口中找到标签?

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

I'm trying to debug a little test driver application written in C#. 我正在尝试调试一些用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. 测试应用连接到服务器,连接成功后,在标签中显示“ REMOTE_CONNECTED”。 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. 这个connHwnd总是会返回一个IntPtr.Zero,打印到控制台的Hwnd是我期望测试应用程序的句柄。 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. 我在屏幕上看到标签显示“ REMOTE_CONNECTED”,但该功能找不到它。

Furthermore, other folks have gotten this to work on XP (whereas I'm on Windows 7). 此外,其他人也可以在XP上使用它(而我在Windows 7上)。

Finally, if I run this in Visual Studio and set some breakpoints, then it seems to work. 最后,如果我在Visual Studio中运行它并设置一些断点,那么它似乎可以工作。 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.!). 我的MainWindowHandle未设置为我期望的窗口(我使用Spy ++弄清楚了,谢谢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. 因此,我使用EnumWindows编写了一些代码来查找我要查找的窗口(请参阅链接文章中的约书亚的答案),然后使用该窗口句柄传递给FindWindowEx,一切按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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