protected override void OnControlRemoved(ControlEventArgs e) { base.OnControlRemoved(e); if (_previewHandler != null) { _previewHandler.Unload(); _previewHandler = null; } }
public static IPreviewHandler InitalizePreviewHandler(FileInfoEx file) { IPreviewHandler retVal = null; PreviewerInfo previewer = getPreviewer(file.FullName); if (previewer != null) { bool isInitialized = false; Type a = Type.GetTypeFromCLSID(previewer.CLSID, true); object o = Activator.CreateInstance(a); IInitializeWithFile fileInit = o as IInitializeWithFile; IInitializeWithStream streamInit = o as IInitializeWithStream; if (fileInit != null) { fileInit.Initialize(file.FullName, 0); isInitialized = true; } else if (streamInit != null) { FileStreamEx stream = file.OpenRead(); streamInit.Initialize((IStream)stream, 0); isInitialized = true; } if (isInitialized) { retVal = o as IPreviewHandler; } } return(retVal); }
public TypedPreviewerHandle(ShellPreviewControl target, Guid clsid) { this.Parent = target; this.CLSID = clsid; Handler = (IPreviewHandler)CoCreateInstance(clsid, null, CLSCTX.LOCAL_SERVER, typeof(IPreviewHandler).GUID); }
public void AttachPreview(IntPtr handler, string fileName, Rect viewRect) { Release(); string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f"; Guid g = new Guid(CLSID); string[] exts = fileName.Split('.'); string ext = exts[exts.Length - 1]; using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(string.Format(@".{0}\ShellEx\{1:B}", ext, g))) { if (hk != null) { g = new Guid(hk.GetValue("").ToString()); Type type = Type.GetTypeFromCLSID(g, true); object comObj = Activator.CreateInstance(type); IInitializeWithFile fileInit = comObj as IInitializeWithFile; IInitializeWithStream streamInit = comObj as IInitializeWithStream; bool isInitialized = false; try { if (fileInit != null) { fileInit.Initialize(fileName, 0); isInitialized = true; } else if (streamInit != null) { stream = new COMStream(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)); streamInit.Initialize((IStream)stream, 0); isInitialized = true; } } catch (Exception ex) { Logging.Log.Warn("Unable to initialize IPreviewHandler", ex); } pHandler = comObj as IPreviewHandler; if (isInitialized && pHandler != null) { RECT r = new RECT(viewRect); pHandler.SetWindow(handler, ref r); pHandler.SetRect(ref r); pHandler.DoPreview(); } else { Marshal.FinalReleaseComObject(comObj); Release(); } } } }
public PreviewHandler(Guid clsid, IPreviewHandlerManagedFrame frame) { disposed = true; init = false; shown = false; comSite = new PreviewHandlerFrame(frame); site = frame; try { SetupHandler(clsid); disposed = false; } catch { if (previewHandler != null) { Marshal.ReleaseComObject(previewHandler); } previewHandler = null; if (pPreviewHandler != IntPtr.Zero) { Marshal.Release(pPreviewHandler); } pPreviewHandler = IntPtr.Zero; comSite.Dispose(); comSite = null; site = null; throw; } }
public static void Unload() { if (pHandler != null) { pHandler.Unload(); pHandler = null; } }
protected override void OnControlRemoved(ControlEventArgs e) { base.OnControlRemoved(e); if (_previewHandler != null) { _previewHandler.Unload(); _previewHandler = null; } }
//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 LocatePreview(IPreviewHandler handler) { var size = handler.ClientSize; var clientsize = _displayControl.ClientSize; var location = _displayControl.Location; location.Offset(clientsize.Width - size.Width - 20, clientsize.Height - size.Height - 2); handler.Location = location; }
public static void AttachPreview(IntPtr handler, FileInfoEx file, Rectangle viewRect) { if (pHandler != null) { pHandler.Unload(); } string fileName = file.FullName; string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f"; Guid g = new Guid(CLSID); string[] exts = fileName.Split('.'); string ext = exts[exts.Length - 1]; using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(string.Format(@".{0}\ShellEx\{1:B}", ext, g))) { if (hk != null) { g = new Guid(hk.GetValue("").ToString()); Type a = Type.GetTypeFromCLSID(g, true); object o = Activator.CreateInstance(a); IInitializeWithFile fileInit = o as IInitializeWithFile; IInitializeWithStream streamInit = o as IInitializeWithStream; bool isInitialized = false; if (fileInit != null) { fileInit.Initialize(fileName, 0); isInitialized = true; } else if (streamInit != null) { FileStreamEx stream = file.OpenRead(); //COMStream stream = new COMStream(File.Open(fileName, FileMode.Open)); streamInit.Initialize((IStream)streamInit, 0); isInitialized = true; } if (isInitialized) { pHandler = o as IPreviewHandler; if (pHandler != null) { ShellAPI.RECT r = new ShellAPI.RECT(viewRect); pHandler.SetWindow(handler, ref r); pHandler.SetRect(ref r); pHandler.DoPreview(); } } } } }
public static void AttachPreview(IntPtr handler, FileInfoEx file, Rectangle viewRect) { if (pHandler != null) { pHandler.Unload(); } string fileName = file.FullName; string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f"; Guid g = new Guid(CLSID); string[] exts = fileName.Split('.'); string ext = exts[exts.Length - 1]; using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(string.Format(@".{0}\ShellEx\{1:B}", ext, g))) { if (hk != null) { g = new Guid(hk.GetValue("").ToString()); Type a = Type.GetTypeFromCLSID(g, true); object o = Activator.CreateInstance(a); IInitializeWithFile fileInit = o as IInitializeWithFile; IInitializeWithStream streamInit = o as IInitializeWithStream; bool isInitialized = false; if (fileInit != null) { fileInit.Initialize(fileName, 0); isInitialized = true; } else if (streamInit != null) { FileStreamEx stream = file.OpenRead(); //COMStream stream = new COMStream(File.Open(fileName, FileMode.Open)); streamInit.Initialize((IStream)streamInit, 0); isInitialized = true; } if (isInitialized) { pHandler = o as IPreviewHandler; if (pHandler != null) { ShellAPI.RECT r = new ShellAPI.RECT(viewRect); pHandler.SetWindow(handler, ref r); pHandler.SetRect(ref r); pHandler.DoPreview(); } } } } }
private void UnloadPreviewHandler() { if (_previewHandler != null) { _previewHandler.Unload(); Marshal.FinalReleaseComObject(_previewHandler); _previewHandler = null; } }
private static bool InitializeWithFile(string pszFile, IPreviewHandler previewHandler) { if (previewHandler is IInitializeWithFile initializeWithFile) { initializeWithFile.Initialize(pszFile, STGM.STGM_READ).ThrowIfFailed("InitializeWithFile::Initialize"); Log.Debug("PreviewHandler::InitializeWithFile"); return(true); } return(false); }
private static bool InitializeWithItem(string pszFile, IPreviewHandler previewHandler) { if (previewHandler is IInitializeWithItem initializeWithItem) { var psi = ShellUtil.GetShellItemForPath(pszFile); initializeWithItem.Initialize(psi, STGM.STGM_READ).ThrowIfFailed("InitializeWithItem::Initialize"); Log.Debug("PreviewHandler::InitializeWithItem"); return(true); } return(false); }
private static bool InitializeWithStream(string pszFile, IPreviewHandler previewHandler) { if (previewHandler is IInitializeWithStream initializeWithStream) { SHCreateStreamOnFile(pszFile, STGM.STGM_READ, out var ppstm).ThrowIfFailed("InitializeWithStream::SHCreateStreamOnFile"); initializeWithStream.Initialize(ppstm, STGM.STGM_READ).ThrowIfFailed("InitializeWithStream::Initialize"); Log.Debug("PreviewHandler::IInitializeWithStream"); return(true); } return(false); }
/// <summary> /// Releases the unmanaged resources used by the PreviewHandlerHost and optionally releases the managed resources. /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { UnloadPreviewHandler(); if (_mCurrentPreviewHandler != null) { Marshal.FinalReleaseComObject(_mCurrentPreviewHandler); _mCurrentPreviewHandler = null; GC.Collect(); } base.Dispose(disposing); }
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); _previewHandler = PreviewHelper.InitalizePreviewHandler(_file); if (_previewHandler != null) { ShellAPI.RECT r = new ShellAPI.RECT(this.ClientRectangle); _previewHandler.SetWindow(this.Handle, ref r); _previewHandler.SetRect(ref r); _previewHandler.DoPreview(); } }
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); _previewHandler = PreviewHelper.InitalizePreviewHandler(_file); if (_previewHandler != null) { ShellAPI.RECT r = new ShellAPI.RECT(this.ClientRectangle); _previewHandler.SetWindow(this.Handle, ref r); _previewHandler.SetRect(ref r); _previewHandler.DoPreview(); } }
public static void Initialize(this IPreviewHandler previewHandler, string filePath, out Stream openedStream) { // File { var iwf = previewHandler as IInitializeWithFile; if (iwf != null) { iwf.Initialize(filePath, 0); openedStream = null; return; } } // Stream { var iws = previewHandler as IInitializeWithStream; if (iws != null) { openedStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); try { iws.Initialize(openedStream.AsIStream(), 0); } catch { openedStream.Dispose(); openedStream = null; throw; } openedStream = null; return; } } // Item { var iwi = previewHandler as IInitializeWithItem; if (iwi != null) { var item = (IShellItem)SHCreateItemFromParsingName(filePath, null, typeof(IShellItem).GUID); iwi.Initialize(item, 0); openedStream = null; return; } } throw new NotSupportedException("Unknown initializer for IPreviewHandler"); }
private void Unload() { if (this.currentHandler != null) { this.currentHandler.Unload(); Marshal.FinalReleaseComObject(this.currentHandler); this.currentHandler = null; } if (this.stream != null) { this.stream.Dispose(); this.stream = null; } }
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; }
public bool Open(string fileName) { UnloadPreviewHandler(); _previewHandler = CreatePreviewHandler(fileName); if (_previewHandler != null) { _previewHandler.SetWindow(Handle, ClientRectangle); _previewHandler.DoPreview(); return(true); } return(false); }
/// <summary> /// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost. /// </summary> /// <param name="path"></param> /// <returns></returns> public bool Open(string path) { UnloadPreviewHandler(); if (string.IsNullOrEmpty(path)) { return(false); } // try to get GUID for the preview handler var guid = GetPreviewHandlerGUID(path); if (guid == Guid.Empty) { return(false); } CurrentPreviewHandler = guid; var o = Activator.CreateInstance(Type.GetTypeFromCLSID(CurrentPreviewHandler, true)); var fileInit = o as IInitializeWithFile; if (fileInit == null) { return(false); } fileInit.Initialize(path, 0); _mCurrentPreviewHandler = o as IPreviewHandler; if (_mCurrentPreviewHandler == null) { return(false); } if (IsDisposed) { return(false); } // bind the preview handler to the control's bounds and preview the content var r = ClientRectangle; _mCurrentPreviewHandler.SetWindow(Handle, ref r); _mCurrentPreviewHandler.DoPreview(); return(true); }
private void Release() { if (pHandler != null) { try { pHandler.Unload(); } catch { } Marshal.FinalReleaseComObject(pHandler); pHandler = null; } if (stream != null) { stream.Dispose(); stream = null; } }
private void UnloadPreviewHandler() { if (_previewHandler != null) { try { _previewHandler.Unload(); } catch { // ignored } finally { Marshal.FinalReleaseComObject(_previewHandler); _previewHandler = null; } } }
public void Dispose() { if (Handler == null) { return; } try { Handler.Unload(); } finally { var temp = Handler; Handler = null; Marshal.FinalReleaseComObject(temp); OpenStream?.Dispose(); OpenStream = null; } }
public void Dispose() { if (Handler == null) { return; } try { // excel is ill behaved (100% CPU) when told to unload, but // FinalReleaseComObject does the trick fine. This seems to // only apply when the hosting window disappears before calling Unload. //Handler.Unload(); } finally { var temp = Handler; Handler = null; Marshal.FinalReleaseComObject(temp); OpenStream?.Dispose(); OpenStream = null; } }
public bool Open(string fileName) { UnloadPreviewHandler(); _previewHandler = CreatePreviewHandler(fileName); if (_previewHandler != null) { _previewHandler.SetWindow(Handle, ClientRectangle); _previewHandler.DoPreview(); return true; } return false; }
private static bool InitPreviewHandler(string fileName, IPreviewHandler previewHandler) { return(InitializeWithStream(fileName, previewHandler) || InitializeWithFile(fileName, previewHandler) || InitializeWithItem(fileName, previewHandler)); }
public void AttachPreview(IntPtr handler, string fileName, Rect viewRect) { Release(); string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f"; Guid g = new Guid(CLSID); string[] exts = fileName.Split('.'); string ext = exts[exts.Length - 1]; using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(string.Format(@".{0}\ShellEx\{1:B}", ext, g))) { if (hk != null) { g = new Guid(hk.GetValue("").ToString()); Type type = Type.GetTypeFromCLSID(g, true); object comObj = Activator.CreateInstance(type); IInitializeWithFile fileInit = comObj as IInitializeWithFile; IInitializeWithStream streamInit = comObj as IInitializeWithStream; bool isInitialized = false; try { if (fileInit != null) { fileInit.Initialize(fileName, 0); isInitialized = true; } else if (streamInit != null) { stream = new COMStream(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)); streamInit.Initialize((IStream)stream, 0); isInitialized = true; } } catch (Exception ex) { Logging.Log.Warn("Unable to initialize IPreviewHandler", ex); } pHandler = comObj as IPreviewHandler; if (isInitialized && pHandler != null) { RECT r = new RECT(viewRect); pHandler.SetWindow(handler, ref r); pHandler.SetRect(ref r); pHandler.DoPreview(); } else { Marshal.FinalReleaseComObject(comObj); Release(); } } } }
public void AttachPreview(IntPtr handler, Rect viewRect, string extension, string filePath, Stream sourceStream) { this.Unload(); var classKey = GetPreviewHandlerKey(extension); if (classKey == null) { return; } var guid = new Guid(classKey.GetValue(string.Empty).ToString()); var type = Type.GetTypeFromCLSID(guid, true); var instance = Activator.CreateInstance(type); var fileInit = instance as IInitializeWithFile; var streamInit = instance as IInitializeWithStream; if (streamInit != null && sourceStream != null) { this.stream = new InteropStream(sourceStream); streamInit.Initialize(this.stream, 0); } else if (fileInit != null) { if (filePath != null) { fileInit.Initialize(filePath, 0); } else if (sourceStream != null) { using (var tempFile = new TempFile()) { using (var fileStream = File.Create(tempFile.Path)) sourceStream.CopyTo(fileStream); fileInit.Initialize(tempFile.Path, 0); } } else { return; } } else { return; } this.currentHandler = instance as IPreviewHandler; if (this.currentHandler == null) { this.Unload(); return; } var rect = new ShellRect(viewRect); this.currentHandler.SetWindow(handler, ref rect); this.currentHandler.SetRect(ref rect); try { this.currentHandler.DoPreview(); } catch { this.Unload(); throw; } }
private void UnloadPreviewHandler() { if (_previewHandler != null) { _previewHandler.Unload(); Marshal.FinalReleaseComObject(_previewHandler); _previewHandler = null; } }
public void AttachPreview(IntPtr handler, string fileName, Rect viewRect) { Release(); try { string CLSID = "8895b1c6-b41f-4c1c-a562-0d564250836f"; Guid g = new Guid(CLSID); string[] exts = fileName.Split('.'); string ext = exts[exts.Length - 1]; var regKey = string.Format(@".{0}\ShellEx\{1:B}", ext, g); using (RegistryKey hk = Registry.ClassesRoot.OpenSubKey(regKey)) { if (hk == null) { Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - Registry Key not found: " + regKey); return; } var objValue = hk.GetValue(""); if (objValue == null) { Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - no handler registrated in Registry"); return; } if (!Guid.TryParse(objValue.ToString(), out g)) { Logging.Log.WarnOnce("Unable to initialize IPreviewHandler - cannot parse guid from registry: " + objValue); return; } Type type = Type.GetTypeFromCLSID(g, false); if (type == null) { Logging.Log.WarnOnce(string.Format("Unable to initialize IPreviewHandler - could not load COM Object, Type.GetTypeFromCLSID({0}) returend false", g)); return; } object comObj = Activator.CreateInstance(type); IInitializeWithFile fileInit = comObj as IInitializeWithFile; IInitializeWithStream streamInit = comObj as IInitializeWithStream; IInitializeWithItem itemInit = comObj as IInitializeWithItem; bool isInitialized = false; if (fileInit != null) { fileInit.Initialize(fileName, 0); // 0 = STGM_READ isInitialized = true; } else if (streamInit != null) { stream = new COMStream(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)); streamInit.Initialize((IStream)stream, 0); // 0 = STGM_READ isInitialized = true; } else if (itemInit != null) { IShellItem shellItem; SHCreateItemFromParsingName(fileName, IntPtr.Zero, typeof(IShellItem).GUID, out shellItem); itemInit.Initialize(shellItem, 0); // 0 = STGM_READ isInitialized = true; } pHandler = comObj as IPreviewHandler; if (isInitialized && pHandler != null) { RECT r = new RECT(viewRect); pHandler.SetWindow(handler, ref r); pHandler.SetRect(ref r); pHandler.DoPreview(); } else { Release(); } } } catch (Exception ex) { Logging.Log.Warn("Unable to initialize IPreviewHandler", ex); Release(); } }
private void Release() { if (pHandler != null) { try { pHandler.Unload(); } catch { } Marshal.FinalReleaseComObject(pHandler); pHandler = null; } if (stream != null) { stream.Dispose(); stream = null; } }
public static void Unload() { if (pHandler != null) { pHandler.Unload(); pHandler = null; } }