//private readonly IOleWindow _oleWindow; public PreviewHandler(IPreviewHandlerFrame previewHandlerFrame, string fileName) { _previewHandler = CreatePreviewHandler(fileName); _previewHandlerVisuals = _previewHandler as IPreviewHandlerVisuals; var objectWithSite = _previewHandler as IObjectWithSite; Log.Debug($"objectWithSite: {objectWithSite}"); objectWithSite?.SetSite(previewHandlerFrame).ThrowIfFailed("IObjectWithSite::SetSite"); }
void SetupHandler(Guid clsid) { IntPtr pph; var iid = IPreviewHandlerIid; var cannotCreate = "Cannot create class " + clsid.ToString() + " as IPreviewHandler."; var cannotCast = "Cannot cast class " + clsid.ToString() + " as IObjectWithSite."; var cannotSetSite = "Cannot set site to the preview handler object."; // Important: manully calling CoCreateInstance is necessary. // If we use Activator.CreateInstance(Type.GetTypeFromCLSID(...)), // CLR will allow in-process server, which defeats isolation and // creates strange bugs. HResult hr = CoCreateInstance(ref clsid, IntPtr.Zero, ClassContext.LocalServer, ref iid, out pph); // See https://blogs.msdn.microsoft.com/adioltean/2005/06/24/when-cocreateinstance-returns-0x80080005-co_e_server_exec_failure/ // CO_E_SERVER_EXEC_FAILURE also tends to happen when debugging in Visual Studio. // Moreover, to create the instance in a server at low integrity level, we need // to use another thread with low mandatory label. We keep it simple by creating // a same-integrity object. if (hr == HResult.CO_E_SERVER_EXEC_FAILURE) { hr = CoCreateInstance(ref clsid, IntPtr.Zero, ClassContext.LocalServer, ref iid, out pph); } if ((int)hr < 0) { throw new COMException(cannotCreate, (int)hr); } pPreviewHandler = pph; var previewHandlerObject = Marshal.GetUniqueObjectForIUnknown(pph); previewHandler = previewHandlerObject as IPreviewHandler; if (previewHandler == null) { Marshal.ReleaseComObject(previewHandlerObject); throw new COMException(cannotCreate); } var objectWithSite = previewHandlerObject as IObjectWithSite; if (objectWithSite == null) { throw new COMException(cannotCast); } hr = objectWithSite.SetSite(comSite); if ((int)hr < 0) { throw new COMException(cannotSetSite, (int)hr); } visuals = previewHandlerObject as IPreviewHandlerVisuals; }