private void ReleaseAxControl() { // This line is like a bit of magic... // sometimes, we crash with it on, // sometimes, with it off... // Lately, I have decided to leave it on... // (oh, yes, and the crashes seemed to disappear...) //cpr: ComLib.Release(instance); this.NoComponentChangeEvents++; ContainerControl f = ContainingControl; if (f != null) { f.VisibleChanged -= this.onContainerVisibleChanged; } try { if (instance != null) { Marshal.FinalReleaseComObject(instance); instance = null; iOleInPlaceObject = null; iOleObject = null; iOleControl = null; iOleInPlaceActiveObject = null; iOleInPlaceActiveObjectExternal = null; iPerPropertyBrowsing = null; iCategorizeProperties = null; iPersistStream = null; iPersistStreamInit = null; iPersistStorage = null; } axState[checkedIppb] = false; axState[checkedCP] = false; axState[disposed] = true; freezeCount = 0; axState[sinkAttached] = false; wndprocAddr = IntPtr.Zero; SetOcState(OC_PASSIVE); } finally { this.NoComponentChangeEvents--; } }
private void DepersistControl() { Freeze(true); if (ocxState == null) { // must init new: // if (instance is UnsafeNativeMethods.IPersistStreamInit) { iPersistStreamInit = (UnsafeNativeMethods.IPersistStreamInit) instance; try { storageType = STG_STREAMINIT; iPersistStreamInit.InitNew(); } catch (Exception e1) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStreamInit.InitNew(). Is this good?" + e1.ToString()); } return; } if (instance is UnsafeNativeMethods.IPersistStream) { storageType = STG_STREAM; iPersistStream = (UnsafeNativeMethods.IPersistStream) instance; return; } if (instance is UnsafeNativeMethods.IPersistStorage) { storageType = STG_STORAGE; ocxState = new State(this); iPersistStorage = (UnsafeNativeMethods.IPersistStorage) instance; try { iPersistStorage.InitNew(ocxState.GetStorage()); } catch (Exception e2) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStorage.InitNew(). Is this good?" + e2.ToString()); } return; } if (instance is UnsafeNativeMethods.IPersistPropertyBag) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, this + " supports IPersistPropertyBag."); iPersistPropBag = (UnsafeNativeMethods.IPersistPropertyBag) instance; try { iPersistPropBag.InitNew(); } catch (Exception e1) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistPropertyBag.InitNew(). Is this good?" + e1.ToString()); } } Debug.Fail("no implemented persitance interfaces on object"); throw new InvalidOperationException(SR.GetString(SR.UnableToInitComponent)); } // Otherwise, we have state to deperist from: switch (ocxState.Type) { case STG_STREAM: try { iPersistStream = (UnsafeNativeMethods.IPersistStream) instance; DepersistFromIStream(ocxState.GetStream()); } catch (Exception e) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStream.DepersistFromIStream(). Is this good?" + e.ToString()); } break; case STG_STREAMINIT: if (instance is UnsafeNativeMethods.IPersistStreamInit) { try { iPersistStreamInit = (UnsafeNativeMethods.IPersistStreamInit) instance; DepersistFromIStreamInit(ocxState.GetStream()); } catch (Exception e) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStreamInit.DepersistFromIStreamInit(). Is this good?" + e.ToString()); } GetControlEnabled(); } else { ocxState.Type = STG_STREAM; DepersistControl(); return; } break; case STG_STORAGE: try { iPersistStorage = (UnsafeNativeMethods.IPersistStorage) instance; DepersistFromIStorage(ocxState.GetStorage()); } catch (Exception e) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistStorage.DepersistFromIStorage(). Is this good?" + e.ToString()); } break; default: Debug.Fail("unknown storage type."); throw new InvalidOperationException(SR.GetString(SR.UnableToInitComponent)); } if (ocxState.GetPropBag() != null) { try { iPersistPropBag = (UnsafeNativeMethods.IPersistPropertyBag)instance; DepersistFromIPropertyBag(ocxState.GetPropBag()); } catch (Exception e) { Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Exception thrown trying to IPersistPropertyBag.DepersistFromIPropertyBag(). Is this good?" + e.ToString()); } } }