/// <summary>
        /// Read the registry to learn about the preview handlers that are available on this machine
        /// Return a structure containing 2 collections.  One of all the file extensions and whether we found a preview handler
        /// registered, and one of the preview handlers and their CLSIDs
        /// </summary>
        internal static RegistrationData LoadRegistrationInformation()
        {
            // Load and sort all preview handler information from registry
            List<PreviewHandlerInfo> handlers = new List<PreviewHandlerInfo>();
            using (RegistryKey handlersKey = Registry.LocalMachine.OpenSubKey(
                BaseRegistryKey))
            {
                foreach (string id in handlersKey.GetValueNames())
                {
                    PreviewHandlerInfo handler = new PreviewHandlerInfo();
                    handler.ID = id;
                    handler.Name = handlersKey.GetValue(id, null) as string;
                    handlers.Add(handler);
                }
            }
            handlers.Sort(delegate(PreviewHandlerInfo first, PreviewHandlerInfo second)
            {
                if (first.Name == null) return 1;
                else if (second.Name == null) return -1;
                else return first.Name.CompareTo(second.Name);
            });

            // Create a lookup table of preview handler ID -> PreviewHandlerInfo
            Dictionary<string, PreviewHandlerInfo> handlerMapping = new Dictionary<string, PreviewHandlerInfo>(handlers.Count);
            foreach (PreviewHandlerInfo handler in handlers)
            {
                handlerMapping.Add(handler.ID, handler);
            }

            // Get all classes/extensions from registry
            string[] extensions = Registry.ClassesRoot.GetSubKeyNames();

            // Find out what each extension is registered to be previewed with
            List<ExtensionInfo> extensionInfos = new List<ExtensionInfo>(extensions.Length);
            foreach (string extension in extensions)
            {
                if (extension.StartsWith("."))
                {
                    ExtensionInfo info = new ExtensionInfo();
                    info.Extension = extension;

                    string id = Registry.GetValue(
                        string.Format(BaseClsIDKey, extension),
                        null, null) as string;
                    if (id == null)
                        id = Registry.GetValue(
                        string.Format(BaseClsIdKey2, extension),
                        null, null) as string;
                    PreviewHandlerInfo mappedHandler;
                    if (id != null && handlerMapping.TryGetValue(id, out mappedHandler)) info.Handler = mappedHandler;

                    extensionInfos.Add(info);
                }
            }

            // Return the information
            RegistrationData data = new RegistrationData();
            data.Handlers = handlers;
            data.Extensions = extensionInfos;
            return data;
        }
示例#2
0
        /// <summary>
        /// 1) Look up the preview handler associated with the file extension
        /// 2) Create an instance of the handler using its CLSID and reflection
        /// 3) Check if it is a file or stream handler
        /// 4) Initialize with File or Stream using Initialize from the appropriate interface
        /// 5) Call SetWindow passing in a handle to this control and the bounds of the control
        /// 6) Call DoPreview
        /// </summary>
        private void GeneratePreview()
        {
            lblMessage.Visible = false;
            if (_comInstance != null)
            {
                ((IPreviewHandler)_comInstance).Unload();
            }

            RECT r;

            r.top    = 0;
            r.bottom = this.Height;
            r.left   = 0;
            r.right  = this.Width;

            RegistrationData   data    = PreviewHandlerRegistryAccessor.LoadRegistrationInformation();
            PreviewHandlerInfo handler = null;

            foreach (ExtensionInfo ei in data.Extensions)
            {
                if (_filePath.ToUpper().EndsWith(ei.Extension.ToUpper()))
                {
                    handler = ei.Handler;
                    if (handler != null)
                    {
                        break;
                    }
                }
            }
            if (handler == null)
            {
                lblMessage.Visible = true;
                lblMessage.Text    = "No Preview Available";
                return;
            }

            Type comType = Type.GetTypeFromCLSID(new Guid(handler.ID));

            try
            {
                // Create an instance of the preview handler
                _comInstance = Activator.CreateInstance(comType);

                // Check if it is a stream or file handler
                if (_comInstance is IInitializeWithFile)
                {
                    ((IInitializeWithFile)_comInstance).Initialize(_filePath, 0);
                }
                else if (_comInstance is IInitializeWithStream)
                {
                    if (File.Exists(_filePath))
                    {
                        StreamWrapper stream = new StreamWrapper(File.Open(_filePath, FileMode.Open));
                        ((C4F.DevKit.PreviewHandler.PreviewHandlerFramework.IInitializeWithStream)_comInstance).Initialize(stream, 0);
                    }
                    else
                    {
                        throw new Exception("File not found");
                    }
                }
                ((IPreviewHandler)_comInstance).SetWindow(this.Handle, ref r);
                ((IPreviewHandler)_comInstance).DoPreview();
            }
            catch (Exception ex)
            {
                _comInstance       = null;
                lblMessage.Visible = true;
                lblMessage.Text    = "Preview Generation Failed - " + ex.Message;
            }
        }