4

I am having trouble ensuring a hosted window is correctly destroyed.

I have a HwndHost-derived class that I am displaying in a TabControl (though that is probably irrelevant). I am trying to destroy the hosted content when the tab closes (not when the containing window closes.)

I currently have code to the effect of myControlHost.Dispose(), which ensures that HwndHost.DestroyWindowCore is called immediately. The problem is, DestroyWindowCore does not actually destroy the hosted HWND content!

I would have thought that this was enough to ensure that the underlying CWnd-derived application receives a WM_CLOSE or something, but this does not seem to happen - Spy++ reports only a registered message "HwndSubclass.DetachMessage" being sent.

I have read that you are not supposed to explicitly send your hosted window a WM_CLOSE in the DestroyWindowCore, as this is supposed to happen automatically.

What is the correct way to ensure a hosted window is correctly destroyed when manually removing a HwndHost-derived control?

Cechner
  • 849
  • 7
  • 19

1 Answers1

3

According to this MSDN document, they are calling DestroyWindow() in DestroyWindowCore: http://msdn.microsoft.com/en-us/library/ms752055.aspx

DestroyWindow() will post WM_CLOSE message into message queue, so actually you don't need or should not directly send/post WM_CLOSE.

In my application, actually I am calling DestroyWindow() in a sub DLL which is called from C# side in DestroyWindowCore callback. Then, everything is working fine.

Aki24x
  • 1,058
  • 1
  • 13
  • 28
  • Thanks, I ended up calling `DestroyWindow()` from `DestroyWindowCore()` in the end, but didn't want to update this article until I found the original reference that stated that you shouldn't do this... got lazy though. I suspect I either misunderstood the documentation or that the documentation was incorrect (it was actually just a comment in a code sample) – Cechner May 11 '11 at 00:59