Send message to a non-existent window

Posted on 2006-06-05
Last Modified: 2013-11-20
In my application, Dialog "A" can indirectly create Dialog "B".  In a perfect situation, Dialog "A" will wait for Dialog "B" to signal completion by sending some completion information via PostMessage.  However, if the user insists on exiting from Dialog "A" without first cleaning up Dialog "B", then Dialog "B", upon completion, will be Posting a message into the ether.

My questions:

1.  Is is OK to post a message to a handle that is no longer in existence.

2.  How often will a new window get the same HWND as a previous window.  I'm concerned that since Windows maintains some sort of HWND table(I'm guessing), the addresses are re-used often.

3.  Is there a better way to do what I want to do here.  Dialog "B" might be creating a new record in a database file, and Dialog "A", assuming it is still in existence, should not only know that Dialog "B" has completed, but should also know the location of the new record, and come back into focus.  If the user has cancelled Dialog "A", it does not necessarily invalidate the action of Dialog "B", it just renders the message from Dialog "B" moot.  I intend to warn the user, but, a willy nilly user could potentially create 4 or 5 Dialog "Bs" this way, and I don't want to put the onus on him/her to clean up the mess before closing Dialog "A".

Thanks in advance, Rick
Question by:rickatseasoft
    LVL 48

    Expert Comment

    If you have window handle and don't know exactly whether window exists, you can do the following:

    if ( ::IsWindow(hwnd) )
        PostMessage(hWnd, ...);

    MFC way:

    if ( ::IsWindow(pWnd->m_hWnd) )
    LVL 48

    Accepted Solution

    To prevent this situation you can post notification message from A to B when A is closed. After receiving this message B knows that A doesn't exist.

    About reusing window handle: I think you can be sure that handle is unique all time window exists. If window is destroyed, handle can be reused.
    LVL 43

    Expert Comment

    Have a look at RegisterWindowMessage in help.  You should not need to worry about window handles being re-used as there won't be a handler for the message you post when it isn't the dialog you expect it to be.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now