示例#1
0
        private static void ThrowIfInvalid(Type type, ThumbnailProviderAttribute attribute)
        {
            Type[] interfaces = type.GetInterfaces();
            bool   interfaced = interfaces.Any(x => x == typeof(IThumbnailFromStream));

            if (interfaces.Any(x => x == typeof(IThumbnailFromShellObject) || x == typeof(IThumbnailFromFile)))
            {
                // According to MSDN (http://msdn.microsoft.com/en-us/library/cc144114(v=VS.85).aspx)
                // A thumbnail provider that does not implement IInitializeWithStream must opt out of
                // running in the isolated process. The default behavior of the indexer opts in
                // to process isolation regardless of which interfaces are implemented.
                if (!interfaced && !attribute.DisableProcessIsolation)
                {
                    throw new InvalidOperationException(
                              string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            LocalizedMessages.ThumbnailProviderDisabledProcessIsolation,
                                            type.Name));
                }

                interfaced = true;
            }

            if (!interfaced)
            {
                throw new InvalidOperationException(
                          string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                        LocalizedMessages.ThumbnailProviderInterfaceNotImplemented,
                                        type.Name));
            }
        }
        private static void RegisterThumbnailHandler(string guid, ThumbnailProviderAttribute attribute)
        {
            // set process isolation
            using (RegistryKey clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"))
                using (RegistryKey guidKey = clsidKey.OpenSubKey(guid, true))
                {
                    guidKey.SetValue("DisableProcessIsolation", attribute.DisableProcessIsolation ? 1 : 0, RegistryValueKind.DWord);

                    using (RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true))
                    {
                        inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String);
                    }
                }

            // register file as an approved extension
            using (RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey(
                       @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true))
            {
                approvedShellExtensions.SetValue(guid, attribute.Name, RegistryValueKind.String);
            }

            // register extension with each extension in the list
            string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string extension in extensions)
            {
                using (RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension)) // Create makes it writable
                    using (RegistryKey shellExKey = extensionKey.CreateSubKey("shellex"))
                        using (RegistryKey providerKey = shellExKey.CreateSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B")))
                        {
                            providerKey.SetValue(null, guid, RegistryValueKind.String);

                            if (attribute.ThumbnailCutoff == ThumbnailCutoffSize.Square20)
                            {
                                extensionKey.DeleteValue("ThumbnailCutoff", false);
                            }
                            else
                            {
                                extensionKey.SetValue("ThumbnailCutoff", (int)attribute.ThumbnailCutoff, RegistryValueKind.DWord);
                            }


                            if (attribute.TypeOverlay != null)
                            {
                                extensionKey.SetValue("TypeOverlay", attribute.TypeOverlay, RegistryValueKind.String);
                            }

                            if (attribute.ThumbnailAdornment == ThumbnailAdornment.Default)
                            {
                                extensionKey.DeleteValue("Treatment", false);
                            }
                            else
                            {
                                extensionKey.SetValue("Treatment", (int)attribute.ThumbnailAdornment, RegistryValueKind.DWord);
                            }
                        }
            }
        }
 private static void Unregister(Type registerType)
 {
     if (registerType != null && registerType.IsSubclassOf(typeof(ThumbnailProvider)))
     {
         object[] attributes = registerType.GetCustomAttributes(typeof(ThumbnailProviderAttribute), true);
         if (attributes != null && attributes.Length == 1)
         {
             ThumbnailProviderAttribute attribute = attributes[0] as ThumbnailProviderAttribute;
             UnregisterThumbnailHandler(registerType.GUID.ToString("B"), attribute);
         }
     }
 }
        private static void UnregisterThumbnailHandler(string guid, ThumbnailProviderAttribute attribute)
        {
            string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string extension in extensions)
            {
                using (RegistryKey extKey = Registry.ClassesRoot.OpenSubKey(extension, true))
                    using (RegistryKey shellexKey = extKey.OpenSubKey("shellex", true))
                    {
                        shellexKey.DeleteSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B"), false);

                        extKey.DeleteValue("ThumbnailCutoff", false);
                        extKey.DeleteValue("TypeOverlay", false);
                        extKey.DeleteValue("Treatment", false); // Thumbnail adornment
                    }
            }

            using (RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey(
                       @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true))
            {
                approvedShellExtensions.DeleteValue(guid, false);
            }
        }
示例#5
0
        private static void ThrowIfInvalid(Type type, ThumbnailProviderAttribute attribute)
        {
            var interfaces = type.GetInterfaces();
            bool interfaced = interfaces.Any(x => x == typeof(IThumbnailFromStream));

            if (interfaces.Any(x => x == typeof(IThumbnailFromShellObject) || x == typeof(IThumbnailFromFile)))
            {
                // According to MSDN (http://msdn.microsoft.com/en-us/library/cc144114(v=VS.85).aspx)
                // A thumbnail provider that does not implement IInitializeWithStream must opt out of 
                // running in the isolated process. The default behavior of the indexer opts in
                // to process isolation regardless of which interfaces are implemented.                
                if (!interfaced && !attribute.DisableProcessIsolation)
                {
                    throw new InvalidOperationException(
                        string.Format(System.Globalization.CultureInfo.InvariantCulture,
                        LocalizedMessages.ThumbnailProviderDisabledProcessIsolation,
                        type.Name));
                }
                interfaced = true;
            }

            if (!interfaced)
            {
                throw new InvalidOperationException(
                        string.Format(System.Globalization.CultureInfo.InvariantCulture,
                        LocalizedMessages.ThumbnailProviderInterfaceNotImplemented,
                        type.Name));
            }
        }
示例#6
0
        private static void UnregisterThumbnailHandler(string guid, ThumbnailProviderAttribute attribute)
        {
            string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string extension in extensions)
            {
                using (RegistryKey extKey = Registry.ClassesRoot.OpenSubKey(extension, true))
                using (RegistryKey shellexKey = extKey.OpenSubKey("shellex", true))
                {
                    shellexKey.DeleteSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B"), false);

                    extKey.DeleteValue("ThumbnailCutoff", false);
                    extKey.DeleteValue("TypeOverlay", false);
                    extKey.DeleteValue("Treatment", false); // Thumbnail adornment
                }
            }

            using (RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey(
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true))
            {
                approvedShellExtensions.DeleteValue(guid, false);
            }
        }
示例#7
0
        private static void RegisterThumbnailHandler(string guid, ThumbnailProviderAttribute attribute)
        {
            // set process isolation
            using (RegistryKey clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"))
            using (RegistryKey guidKey = clsidKey.OpenSubKey(guid, true))
            {
                guidKey.SetValue("DisableProcessIsolation", attribute.DisableProcessIsolation ? 1 : 0, RegistryValueKind.DWord);

                using (RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true))
                {
                    inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String);
                }
            }

            // register file as an approved extension
            using (RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey(
                 @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true))
            {
                approvedShellExtensions.SetValue(guid, attribute.Name, RegistryValueKind.String);
            }

            // register extension with each extension in the list
            string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string extension in extensions)
            {
                using (RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension)) // Create makes it writable
                using (RegistryKey shellExKey = extensionKey.CreateSubKey("shellex"))
                using (RegistryKey providerKey = shellExKey.CreateSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B")))
                {
                    providerKey.SetValue(null, guid, RegistryValueKind.String);

                    if (attribute.ThumbnailCutoff == ThumbnailCutoffSize.Square20)
                    {
                        extensionKey.DeleteValue("ThumbnailCutoff", false);
                    }
                    else
                    {
                        extensionKey.SetValue("ThumbnailCutoff", (int)attribute.ThumbnailCutoff, RegistryValueKind.DWord);
                    }


                    if (attribute.TypeOverlay != null)
                    {
                        extensionKey.SetValue("TypeOverlay", attribute.TypeOverlay, RegistryValueKind.String);
                    }

                    if (attribute.ThumbnailAdornment == ThumbnailAdornment.Default)
                    {
                        extensionKey.DeleteValue("Treatment", false);
                    }
                    else
                    {
                        extensionKey.SetValue("Treatment", (int)attribute.ThumbnailAdornment, RegistryValueKind.DWord);
                    }
                }
            }
        }