开发者

FindWindowEx not able to find window handle

I am trying to find the handle to a dialog as soon as it open开发者_如何转开发s.

Now as soon as dialog is opened I try to call FindWindowEx for that dialog in a separate thread but it returns NULL.

I then put some sleep before calling FindWindowEx. It works some time after putting sleep.

It looks like FindWindowEx is getting called before even dialog is created and sleep is helping to create the dialog and hence some times it work.

Now I have put some random value in sleep. And it doesnot look a good approach since it can fail anytime.

Is there any full proof approach so that I may get handle every time via FindWindowEx without making thread to sleep.


If the dialog you are looking for is your dialog -- that is, you control the code -- then you can send a message from your dialog to your watching app that says, "Oh, hi there!"

If the dialog is not yours, and you don't want to spin, you can create a Windows hook on the WM_CREATE message.


A very straightforward solution would be to call FindWindowEx repeatedly in a loop.

HWND h = NULL;
while (1) {
      h = FindWindowEx(...);
      if (h) {
         break;
      } 
      Sleep(100);
   }

That's not bullet-proof - it's an infinite loop if the dialog nevers opens, or is closed too quickly (although that's unlikely). To catch both cases, let the main thread (which creates and runs the dialog) maintain a simple bool property that the worker thread queries to find out whether there's still a dialog around.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜