none
How to check if window still exists? RRS feed

  • Question

  • Suppose I have a handle to a window,and I need to check if it still exists,how do I do that?

    The obvious way is with IsWindow(),but I heard that handles get reused,so by the time i check to see if the window still exists,another new window might get it's hwnd,so that might trick me.

    What's the proper way to do this?

    Tuesday, May 7, 2013 7:02 PM

Answers

  • On 5/7/2013 6:24 PM, Turda Alexander wrote:

    I have to send messages to it and I'm gonna use it with hooking in my app.It's gonna check the hwnds the hooking technique gets me against the hwnd of the window to see if they match.

    Can't that "hooking technique" also notify you that the window is being destroyed?


    Igor Tandetnik

    Tuesday, May 7, 2013 10:35 PM
  • To distinguish the window, try setting a custom property, like ‘SetProp(hwnd, _T("MyProp"), (HANDLE)1234)’. But you cannot be always sure that the handle was not reused between checking the window and sending a message.

    Wednesday, May 8, 2013 5:20 AM

All replies

  • On 5/7/2013 3:02 PM, Turda Alexander wrote:

    Suppose I have a handle to a window,and I need to check if it still exists,how do I do that?

    The obvious way is with IsWindow(),but I heard that handles get reused,so by the time i check to see if the window still exists,another new window might get it's hwnd,so that might trick me.

    Well, at this point, you would have a handle to a window that exists. So IsWindow() does give you a correct answer to your question. What again seems to be the problem?


    Igor Tandetnik

    Tuesday, May 7, 2013 7:22 PM
  • What if in the meantime the window closes,and I don't test it at that time,but after it closes,a new one is created with the same handle,and I test it at that time,it will give me big problems.
    Tuesday, May 7, 2013 9:01 PM
  • On 5/7/2013 5:01 PM, Turda Alexander wrote:

    What if in the meantime the window closes,and I don't test it at that time,but after it closes,a new one is created with the same handle,and I test it at that time,it will give me big problems.

    Well, what's so special about the original window? Why do you care about it, and not any other? Check that the window is still special in the same way.


    Igor Tandetnik

    Tuesday, May 7, 2013 9:19 PM
  • What if in the meantime the window closes,and I don't test it at that time,but after it closes,a new one is created with the same handle,and I test it at that time,it will give me big problems.
    You've missed Igor's point. What "big problems" will there be if the handle is valid for a new window? What is it about the window that makes it special to your application?
    Tuesday, May 7, 2013 10:00 PM
  • I have to send messages to it and I'm gonna use it with hooking in my app.It's gonna check the hwnds the hooking technique gets me against the hwnd of the window to see if they match.

    I also have to send certain messages to the window to position the cursor and perform mouse clicks.

    Tuesday, May 7, 2013 10:24 PM
  • On 5/7/2013 6:24 PM, Turda Alexander wrote:

    I have to send messages to it and I'm gonna use it with hooking in my app.It's gonna check the hwnds the hooking technique gets me against the hwnd of the window to see if they match.

    Can't that "hooking technique" also notify you that the window is being destroyed?


    Igor Tandetnik

    Tuesday, May 7, 2013 10:35 PM
  • To distinguish the window, try setting a custom property, like ‘SetProp(hwnd, _T("MyProp"), (HANDLE)1234)’. But you cannot be always sure that the handle was not reused between checking the window and sending a message.

    Wednesday, May 8, 2013 5:20 AM