问题
I'm having problems with my .net controls not getting cleaned up properly when wrapped for activeX use.
The default behavior leaves the SDK's test container app (TstCon32.exe) running as a GUIless process when I try and close it.
The workaround I initially found via google was to override WndProc and call Environment.Exit(0)
manually. This did get TstCon32.exe to shut down completely; however it's breaking the application where i need to have the control hosted.
The App is MDI and WM_DESTROY is being sent when the page containing the control is closed, at which point the Environment.Exit(0)
call is blowing away the entire app. I've tried Application.Exit()
as an alternative, but that leaves TstCon32 still running invisibly.
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// WM_DESTROY
if (m.Msg == 2)
Environment.Exit(0);
}
回答1:
Generally an ActiveX container would call IOleObject::Close and IOleObject::SetClientSite(null) before closing. System.Windows.Forms.Control has its own implementation of IOleObject. I do not think you can override it in a derived class.
Sending WM_QUIT via Application::Exit is not an option for MDI, as it will close the whole program instead of the page hosting the ActiveX. If the container is leaking interface pointers there is not much you can do.
System.Windows.Forms.Control's IOleObject implementation calls Control.Dispose only if the container implements IHTMLDocument2. But I do not think your can count on the container to implement this interface.
来源:https://stackoverflow.com/questions/3390571/what-is-the-proper-way-for-an-activex-com-control-to-clean-up-when-closed-by-i