• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

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
  • 2
1 Solution
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) )
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.
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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now