What is the proper way for an activeX (COM) control to clean up when closed by its parent?

♀尐吖头ヾ 提交于 2019-12-11 03:38:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!