Exemple #1
0
        internal static void CustomRegisterFunction(Type serverType, RegistrationType registrationType)
        {
            //  We will use the display name a few times.
            var displayName = DisplayNameAttribute.GetDisplayNameOrTypeName(serverType);

            //  Open the local machine.
            using (var localMachineBaseKey = registrationType == RegistrationType.OS64Bit
                ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) :
                                             RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
            {
                //  Open the Preview Handlers.
                using (var previewHandlersKey = localMachineBaseKey
                                                .OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\PreviewHandlers",
                                                            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey))
                {
                    //  If we don't have the key, we've got a problem.
                    if (previewHandlersKey == null)
                    {
                        throw new InvalidOperationException("Cannot open the PreviewHandlers key.");
                    }

                    //  Write the server guid as a name, and the display name as the value.
                    //  The display name isn't needed, it's just helpful for debugging and checking the registry.
                    previewHandlersKey.SetValue(serverType.GUID.ToRegistryString(), displayName);
                }
            }

            //  Open the classes root.
            using (var classesBaseKey = registrationType == RegistrationType.OS64Bit
                ? RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64) :
                                        RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
            {
                //  Our server guid.
                var serverGuid = serverType.GUID.ToRegistryString();

                //  Open the Class Key.
                using (var classKey = classesBaseKey
                                      .OpenSubKey(string.Format(@"CLSID\{0}", serverGuid),
                                                  RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey))
                {
                    //  If we don't have the key, we've got a problem.
                    if (classKey == null)
                    {
                        throw new InvalidOperationException("Cannot open the class key.");
                    }

                    //  Set the AppID as the preview host surrogate.
                    classKey.SetValue(null, serverType.Name);
                    classKey.SetValue("AppID", "{6d2b5079-2f0b-48dd-ab7f-97cec514d30b}");

                    //  Set the display name and TODO icon.
                    classKey.SetValue("DisplayName", displayName, RegistryValueKind.String);
                    classKey.SetValue("Icon", "%SystemRoot%\\system32\\fontext.dll,10", RegistryValueKind.ExpandString);

                    //  Disable low integrity process isolation - TODO maybe this should be optional.
                    classKey.SetValue("DisableLowILProcessIsolation", 1, RegistryValueKind.DWord);
                }
            }
        }
Exemple #2
0
        internal static void CustomRegisterFunction(Type serverType, RegistrationType registrationType)
        {
            var name = DisplayNameAttribute.GetDisplayNameOrTypeName(serverType);
            var view = registrationType == RegistrationType.OS64Bit
                ? RegistryView.Registry64
                : RegistryView.Registry32;

            using (var rootKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, view))
                using (var key = rootKey.CreateSubKey($@"CLSID\{serverType.GUID:B}") ?? throw new Exception("CLSID Key is null"))
                    using (key.CreateSubKey($@"Implemented Categories\{CategoryManager.CATID_DeskBand:B}") ?? throw new Exception("Category Key is null"))
                    {
                        key.SetValue(null, name);
                    }

            // TODO: It may be worth exploring how this method works and replace the code above with it.
            //   Use the category manager to register this server as a Desk Band.
            //CategoryManager.RegisterComCategory(serverType.GUID, CategoryManager.CATID_DeskBand);
        }
        public static void Register(Type serverType, RegistrationType registrationType)
        {
            //  Get the preview handler attribute. If it is missing, throw a registration exception.
            var previewHandlerAttribute = PreviewHandlerAttribute.GetPreviewHandlerAttribute(serverType);

            if (previewHandlerAttribute == null)
            {
                throw new ServerRegistrationException("The server does not have a [PreviewHandler] attribute set.");
            }

            //  We will use the display name a few times.
            var displayName = DisplayNameAttribute.GetDisplayNameOrTypeName(serverType);

            //  Open the local machine.
            using (var localMachineBaseKey = registrationType == RegistrationType.OS64Bit
                                                 ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                                                           RegistryView.Registry64)
                                                 : RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                                                           RegistryView.Registry32))
            {
                //  Open the Preview Handlers.
                using (var previewHandlersKey = localMachineBaseKey
                                                .OpenSubKey(PreviewHandlersKey,
                                                            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey))
                {
                    //  If we don't have the key, we've got a problem.
                    if (previewHandlersKey == null)
                    {
                        throw new InvalidOperationException("Cannot open the PreviewHandlers key.");
                    }

                    //  Write the server guid as a name, and the display name as the value.
                    //  The display name isn't needed, it's just helpful for debugging and checking the registry.
                    previewHandlersKey.SetValue(serverType.GUID.ToRegistryString(), displayName);
                }
            }

            //  Open the classes root.
            using (var classesBaseKey = registrationType == RegistrationType.OS64Bit
                                            ? RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64)
                                            : RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
            {
                //  Our server guid.
                var serverGuid = serverType.GUID.ToRegistryString();

                //  Open the Class Key.
                using (var classKey = classesBaseKey
                                      .OpenSubKey(string.Format(@"CLSID\{0}", serverGuid),
                                                  RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey))
                {
                    //  If we don't have the key, we've got a problem.
                    if (classKey == null)
                    {
                        throw new InvalidOperationException("Cannot open the class key.");
                    }

                    //  For informational purposes, set the server as the default value for the CLSID.
                    classKey.SetValue(null, serverType.Name);

                    //  Set up the surrogate host app id.
                    switch (previewHandlerAttribute.SurrogateHostType)
                    {
                    case SurrogateHostType.DedicatedPrevhost:
                        //  Get the appid.
                        var appId = GetAppIdForServerClsid(serverType.GUID).ToRegistryString();
                        classKey.SetValue("AppID", appId);
                        CreatePrevhostApp(appId);
                        break;

                    case SurrogateHostType.Prevhost:
                        //  Standard prevhost App ID.
                        classKey.SetValue("AppID", "{6d2b5079-2f0b-48dd-ab7f-97cec514d30b}");
                        break;

                    case SurrogateHostType.Prevhost32On64:
                        //  Specialised prevhost for x86 handler on x64.
                        classKey.SetValue("AppID", "{534A1E02-D58F-44f0-B58B-36CBED287C7C}");
                        break;

                    default:
                        throw new ServerRegistrationException(
                                  string.Format("{0} is not a valid value for the surrogate host type.",
                                                previewHandlerAttribute.SurrogateHostType));
                    }

                    //  Set the display name and TODO icon.
                    classKey.SetValue("DisplayName", displayName, RegistryValueKind.String);
                    classKey.SetValue("Icon", "%SystemRoot%\\system32\\fontext.dll,10", RegistryValueKind.ExpandString);

                    //  Disable low integrity process isolation if specified.
                    if (previewHandlerAttribute.DisableLowILProcessIsolation)
                    {
                        classKey.SetValue("DisableLowILProcessIsolation", 1, RegistryValueKind.DWord);
                    }
                }
            }
        }