Send message to a non-existent window

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
Who is Participating?
AlexFMConnect With a Mentor Commented:
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.
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) )
AndyAinscowFreelance programmer / ConsultantCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.