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(); } } } }
private void Release() { if (pHandler != null) { try { pHandler.Unload(); } catch { } Marshal.FinalReleaseComObject(pHandler); pHandler = null; } if (stream != null) { stream.Dispose(); stream = 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(); } }