WPF interop - HwndHost not automatically destroying the hosted window

2.1k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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.