Esempio n. 1
0
 /// <summary>
 /// Approves an extension.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 /// <exception cref="System.InvalidOperationException">Failed to open the Approved Extensions key.</exception>
 private static void ApproveExtension(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Open the approved extensions key.
     using (var approvedKey = GetApprovedShellExtKey(registrationType)) {
         //  Create an entry for the server.
         approvedKey.SetValue(server.ServerClsid.ToRegistryString(), server.DisplayName);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Unapproves an extension.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 /// <exception cref="System.InvalidOperationException">Failed to open the Approved Extensions key.</exception>
 private static void UnapproveExtension(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Open the approved extensions key.
     using (var approvedKey = GetApprovedShellExtKey(registrationType)) {
         //  Delete the value if it's there.
         approvedKey.DeleteValue(server.ServerClsid.ToRegistryString(), false);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Loads all SharpShell servers from an assembly.
        /// </summary>
        /// <param name="path">The path to the assembly.</param>
        /// <returns>A ServerEntry for each SharpShell server in the assembly.</returns>
        public static IEnumerable <ServerEntry> LoadServers(string path)
        {
            //  Storage for the servers.
            var servers = new List <ServerEntry>();

            try
            {
                //  Create an assembly catalog for the assembly and a container from it.
                var catalog   = new AssemblyCatalog(Path.GetFullPath(path));
                var container = new CompositionContainer(catalog);

                //  Get all exports of type ISharpShellServer.
                var serverTypes = container.GetExports <ISharpShellServer>();

                //  Go through each servertype (creating the instance from the lazy).
                foreach (var serverType in serverTypes)
                {
                    ISharpShellServer server = null;
                    try
                    {
                        server = serverType.Value;
                    }
                    catch (Exception exception)
                    {
                        Trace.TraceError($"An exception occurred loading a server: ${exception.ToString()}");
                        servers.Add(new ServerEntry
                        {
                            ServerName = "Invalid",
                            ServerPath = path,
                            ServerType = ServerType.None,
                            ClassId    = new Guid(),
                            Server     = null,
                            IsInvalid  = true
                        });
                        continue;
                    }

                    //  Yield a server entry for the server type.
                    servers.Add(new ServerEntry
                    {
                        ServerName = server.DisplayName,
                        ServerPath = path,
                        ServerType = server.ServerType,
                        ClassId    = server.ServerClsid,
                        Server     = server
                    });
                }
            }
            catch (Exception exception)
            {
                //  It's almost certainly not a COM server.
                Logging.Error("ServerManager: Failed to load SharpShell server", exception);
                MessageBox.Show("The file '" + Path.GetFileName(path) + "' is not a SharpShell Server.", "Warning");
            }

            //  Return the servers.
            return(servers);
        }
Esempio n. 4
0
        /// <summary>
        /// Unregisters a SharpShell server. This will remove the associations defined by the
        /// server's COMServerAssociation attribute.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration to undo.</param>
        public static void UnregisterServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Unapprove the extension.
            UnapproveExtension(server, registrationType);

            //  Pass the server type to the SharpShellServer internal unregistration function and let it
            //  take over from there.
            SharpShellServer.DoUnregister(server.GetType(), registrationType);
        }
Esempio n. 5
0
 private void CheckIfRegisterOrUnregisterRequiresExplorerRestart(ISharpShellServer server)
 {
     if (server.ServerType == ServerType.ShellIconOverlayHandler)
     {
         if (MessageBox.Show(this, "This change will not take effect until Windows Explorer is restarted. Would you " +
                             "like to restart Windows Explorer now?", "Restart Explorer?", MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question) == DialogResult.Yes)
         {
             ExplorerManager.RestartExplorer();
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Unapproves an extension.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <exception cref="System.InvalidOperationException">Failed to open the Approved Extensions key.</exception>
        private static void UnapproveExtension(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open the approved extensions key.
            using (var approvedKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                                             registrationType == RegistrationType.OS64Bit ? RegistryView.Registry64 : RegistryView.Registry32)
                                     .OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                //  If we can't open the key, we're going to have problems.
                if (approvedKey == null)
                {
                    throw new InvalidOperationException("Failed to open the Approved Extensions key.");
                }

                //  Delete the value if it's there.
                approvedKey.DeleteValue(server.ServerClsid.ToRegistryString(), false);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Uninstalls the server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <returns>True if the server WAS installed and has been uninstalled, false if the server was not found.</returns>
        public static bool UninstallServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                var subKeyTreeName = server.ServerClsid.ToRegistryString();

                //  If the subkey doesn't exist, we can return false - we're already uninstalled.
                if (classesKey.GetSubKeyNames().Any(skn => skn.Equals(subKeyTreeName, StringComparison.OrdinalIgnoreCase)) == false)
                {
                    return(false);
                }

                //  Delete the subkey tree.
                classesKey.DeleteSubKeyTree(subKeyTreeName);
                return(true);
            }
        }
        /// <summary>
        /// Loads all SharpShell servers from an assembly.
        /// </summary>
        /// <param name="path">The path to the assembly.</param>
        /// <returns>A ISharpShellServer for each SharpShell server in the assembly.</returns>
        public static IEnumerable <ISharpShellServer> EnumerateFromFile(string path)
        {
            //  Storage for the servers.
            Lazy <ISharpShellServer>[] serverTypes;

            try
            {
                //  Create an assembly catalog for the assembly and a container from it.
                var catalog   = new AssemblyCatalog(Path.GetFullPath(path));
                var container = new CompositionContainer(catalog);

                //  Get all exports of type ISharpShellServer.
                serverTypes = container.GetExports <ISharpShellServer>().ToArray();
            }
            catch (Exception exception)
            {
                //  It's almost certainly not a COM server.
                Logging.Error("ServerManager: Failed to load SharpShell server", exception);

                throw new BadImageFormatException("The file '" + Path.GetFileName(path) + "' is not a SharpShell Server.", exception);
            }

            //  Go through each servertype (creating the instance from the lazy).
            foreach (var serverType in serverTypes)
            {
                ISharpShellServer server = null;

                try
                {
                    server = serverType.Value;
                }
                catch (Exception exception)
                {
                    Trace.TraceError($"An exception occurred loading a server: ${exception}");
                }

                if (server != null)
                {
                    //  Yield a server entry for the server type.
                    yield return(server);
                }
            }
        }
        /// <summary>
        /// Uninstalls the server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        public static void UninstallServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                var subKeyTreeName = server.ServerClsid.ToRegistryString();

                //  Try and delete the subkey tree. If we fail, it'll be because it's not
                //  there or we don't have permission to delete it.
                try
                {
                    classesKey.DeleteSubKeyTree(subKeyTreeName);
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Trace.WriteLine("Failed to delete server " + server + ". Exception is " + exception);
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Unregisters and uninstalls the shell server
        /// in order to remove functionality
        /// from the windows explorer context menu.
        /// </summary>
        /// <param name="server"></param>
        private void Stop(ISharpShellServer server)
        {
            try
            {
                if (Environment.Is64BitOperatingSystem)
                {
                    ServerRegistrationManager.UninstallServer(server, RegistrationType.OS64Bit);
                    ServerRegistrationManager.UnregisterServer(server, RegistrationType.OS64Bit);
                }
                else
                {
                    ServerRegistrationManager.UninstallServer(server, RegistrationType.OS32Bit);
                    ServerRegistrationManager.UnregisterServer(server, RegistrationType.OS32Bit);
                }

                log.Info(Strings.serviceStopped);
            }
            catch (Exception ex)
            {
                log.Info($"{Strings.stopServiceFail} {ex.Message}");

                throw;
            }
        }
 /// <summary>
 /// Uninstalls the server.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 /// <returns>True if the server WAS installed and has been uninstalled, false if the server was not found.</returns>
 public static bool UninstallServer(ISharpShellServer server, RegistrationType registrationType)
 {
     return(UninstallServer(server.ServerClsid, registrationType));
 }
        /// <summary>
        /// Uninstalls the server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        public static void UninstallServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                var subKeyTreeName = server.ServerClsid.ToRegistryString();

                //  Try and delete the subkey tree. If we fail, it'll be because it's not 
                //  there or we don't have permission to delete it.
                try
                {
                    classesKey.DeleteSubKeyTree(subKeyTreeName);
                }
                catch(Exception exception)
                {
                    System.Diagnostics.Trace.WriteLine("Failed to delete server " + server + ". Exception is " + exception);
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Determines whether a server is associated with a shell item.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="shellItem">The shell item.</param>
        /// <returns>
        ///   <c>true</c> if a server is associated with the shell item; otherwise, <c>false</c>.
        /// </returns>
        private bool IsServerAssociatedWithShellItem(ISharpShellServer server, ShellItem shellItem)
        {
            //  If we don't have the server, bail.
            if (server == null || shellItem == null)
                return false;

            //  Get the associations.
            var associationType = COMServerAssociationAttribute.GetAssociationType(server.GetType());
            var associations = COMServerAssociationAttribute.GetAssociations(server.GetType());

            //  TODO: This is potentially a very useful check - maybe it should be moved into the
            //  COMServerAssociationAttribute class so that it can be reused.

            //  We have a special case for icon overlays.
            if (server is SharpIconOverlayHandler && TestIconOverlayHandler != null && shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
                if (((IShellIconOverlayIdentifier)TestIconOverlayHandler).IsMemberOf(shellItem.Path, FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL) == 0)
                    return true;

            //  Based on the assocation type, we can check the shell item.
            switch (associationType)
            {
                case AssociationType.FileExtension:

                    //  File extensions are easy to check.
                    if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
                    {
                        return
                            associations.Any(
                                a =>
                                string.Compare(Path.GetExtension(shellItem.DisplayName), a,
                                               StringComparison.OrdinalIgnoreCase) == 0);
                    }

                    break;

                case AssociationType.ClassOfExtension:

                    //  TODO must be tested.
                    if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
                    {
                        //  Get our class.
                        var fileClass = ServerRegistrationManager.GetClassForExtension(Path.GetExtension(shellItem.DisplayName));

                        //  Do we match it?
                        return associations.Any(a => string.Compare(fileClass, ServerRegistrationManager.GetClassForExtension(a), StringComparison.InvariantCultureIgnoreCase) == 0);
                    }

                    break;

                case AssociationType.Class:
                    //  TODO must be tested.
                    break;

                case AssociationType.AllFiles:

                    //  TODO must be tested.
                    return shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && shellItem.IsFolder == false;

                case AssociationType.Directory:

                    //  Directories are filesystem, not streams, and folder.
                    return shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && !shellItem.Attributes.HasFlag(SFGAO.SFGAO_STREAM) && shellItem.IsFolder;

                case AssociationType.Drive:

                    //  TODO must be tested.
                    return shellItem.Attributes.HasFlag(SFGAO.SFGAO_STORAGEANCESTOR);

                case AssociationType.UnknownFiles:
                    //  TODO must be tested.
                    break;
            }

            return false;
        }
Esempio n. 14
0
        /// <summary>
        /// Determines whether a server is associated with a shell item.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="shellItem">The shell item.</param>
        /// <returns>
        ///   <c>true</c> if a server is associated with the shell item; otherwise, <c>false</c>.
        /// </returns>
        private bool IsServerAssociatedWithShellItem(ISharpShellServer server, ShellItem shellItem)
        {
            //  If we don't have the server, bail.
            if (server == null || shellItem == null)
            {
                return(false);
            }

            //  Get the associations.
            var associationType = COMServerAssociationAttribute.GetAssociationType(server.GetType());
            var associations    = COMServerAssociationAttribute.GetAssociations(server.GetType());

            //  TODO: This is potentially a very useful check - maybe it should be moved into the
            //  COMServerAssociationAttribute class so that it can be reused.

            //  Based on the assocation type, we can check the shell item.
            switch (associationType)
            {
            case AssociationType.FileExtension:

                //  TODO must be tested.
                //  File extensions are easy to check.
                if (shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM))
                {
                    return
                        (associations.Any(
                             a =>
                             string.Compare(Path.GetExtension(shellItem.DisplayName), "." + a,
                                            StringComparison.OrdinalIgnoreCase) == 0));
                }

                break;

            case AssociationType.ClassOfExtension:

                //  TODO must be tested.
                if (shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM))
                {
                    //  Get our class.
                    var fileClass = ServerRegistrationManager.GetClassForExtension(Path.GetExtension(shellItem.DisplayName));

                    //  Do we match it?
                    return(associations.Any(a => string.Compare(fileClass, ServerRegistrationManager.GetClassForExtension(a), StringComparison.InvariantCultureIgnoreCase) == 0));
                }

                break;

            case AssociationType.Class:
                //  TODO must be tested.
                break;

            case AssociationType.AllFiles:

                //  TODO must be tested.
                return(shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM) && shellItem.IsFolder == false);

            case AssociationType.Directory:

                //  Directories are filesystem, not streams, and folder.
                return(shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM) && !shellItem.Attributes.HasFlag(SFGAOF.SFGAO_STREAM) && shellItem.IsFolder);

            case AssociationType.Drive:

                //  TODO must be tested.
                return(shellItem.Attributes.HasFlag(SFGAOF.SFGAO_STORAGEANCESTOR));

            case AssociationType.UnknownFiles:
                //  TODO must be tested.
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(false);
        }
        /// <summary>
        /// Unapproves an extension.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <exception cref="System.InvalidOperationException">Failed to open the Approved Extensions key.</exception>
        private static void UnapproveExtension(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open the approved extensions key.
            using (var approvedKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                registrationType == RegistrationType.OS64Bit ? RegistryView.Registry64 : RegistryView.Registry32)
                .OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                //  If we can't open the key, we're going to have problems.
                if (approvedKey == null)
                    throw new InvalidOperationException("Failed to open the Approved Extensions key.");

                //  Delete the value if it's there.
                approvedKey.DeleteValue(server.ServerClsid.ToRegistryString(), false);
            }
        }
        /// <summary>
        /// Determines whether a server is associated with a shell item.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="shellItem">The shell item.</param>
        /// <returns>
        ///   <c>true</c> if a server is associated with the shell item; otherwise, <c>false</c>.
        /// </returns>
        private bool IsServerAssociatedWithShellItem(ISharpShellServer server, ShellItem shellItem)
        {
            //  If we don't have the server, bail.
            if (server == null || shellItem == null)
            {
                return(false);
            }

            //  Get the associations.
            var associationType = COMServerAssociationAttribute.GetAssociationType(server.GetType());
            var associations    = COMServerAssociationAttribute.GetAssociations(server.GetType());

            //  TODO: This is potentially a very useful check - maybe it should be moved into the
            //  COMServerAssociationAttribute class so that it can be reused.

            //  We have a special case for icon overlays.
            if (server is SharpIconOverlayHandler && TestIconOverlayHandler != null && shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
            {
                if (((IShellIconOverlayIdentifier)TestIconOverlayHandler).IsMemberOf(shellItem.Path, FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL) == 0)
                {
                    return(true);
                }
            }

            //  Based on the assocation type, we can check the shell item.
            switch (associationType)
            {
            case AssociationType.FileExtension:

                //  File extensions are easy to check.
                if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
                {
                    return
                        (associations.Any(
                             a =>
                             string.Compare(Path.GetExtension(shellItem.DisplayName), a,
                                            StringComparison.OrdinalIgnoreCase) == 0));
                }

                break;

            case AssociationType.ClassOfExtension:

                //  TODO must be tested.
                if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM))
                {
                    //  Get our class.
                    var fileClass = ServerRegistrationManager.GetClassForExtension(Path.GetExtension(shellItem.DisplayName));

                    //  Do we match it?
                    return(associations.Any(a => string.Compare(fileClass, ServerRegistrationManager.GetClassForExtension(a), StringComparison.InvariantCultureIgnoreCase) == 0));
                }

                break;

            case AssociationType.Class:
                //  TODO must be tested.
                break;

            case AssociationType.AllFiles:

                //  TODO must be tested.
                return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && shellItem.IsFolder == false);

            case AssociationType.Directory:

                //  Directories are filesystem, not streams, and folder.
                return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && !shellItem.Attributes.HasFlag(SFGAO.SFGAO_STREAM) && shellItem.IsFolder);

            case AssociationType.Drive:

                //  TODO must be tested.
                return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_STORAGEANCESTOR));

            case AssociationType.UnknownFiles:
                //  TODO must be tested.
                break;
            }

            return(false);
        }
        /// <summary>
        /// Uninstalls the server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <returns>True if the server WAS installed and has been uninstalled, false if the server was not found.</returns>
        public static bool UninstallServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Open classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                var subKeyTreeName = server.ServerClsid.ToRegistryString();

                //  If the subkey doesn't exist, we can return false - we're already uninstalled.
                if (classesKey.GetSubKeyNames().Any(skn => skn.Equals(subKeyTreeName, StringComparison.OrdinalIgnoreCase)) == false)
                    return false;

                //  Delete the subkey tree.
                classesKey.DeleteSubKeyTree(subKeyTreeName);
                return true;
            }
        }
        /// <summary>
        /// Unregisters a SharpShell server. This will remove the associations defined by the
        /// server's COMServerAssociation attribute.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration to undo.</param>
        public static void UnregisterServer(ISharpShellServer server, RegistrationType registrationType)
        {
            //  Unapprove the extension.
            UnapproveExtension(server, registrationType);

            //  Pass the server type to the SharpShellServer internal unregistration function and let it
            //  take over from there.
            SharpShellServer.DoUnregister(server.GetType(), registrationType);
        }
        /// <summary>
        /// Installs a SharpShell COM server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <param name="codeBase">if set to <c>true</c> use code base registration (i.e full assembly path, not the GAC).</param>
        public static void InstallServer(ISharpShellServer server, RegistrationType registrationType, bool codeBase)
        {
            //  Get the server registration information.
            var serverRegistrationInformation = GetServerRegistrationInfo(server, registrationType);

            //  If it is registered, unregister first.
            if (serverRegistrationInformation != null)
                UninstallServer(server, registrationType);

            //  Open the classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                //  Create the server key.
                using (var serverKey = classesKey.CreateSubKey(server.ServerClsid.ToRegistryString()))
                {
                    if(serverKey == null)
                        throw new InvalidOperationException("Cannot create server key.");

                    //  We always set the server key default value to the display name if we can.
                    if(!string.IsNullOrEmpty(server.DisplayName))
                        serverKey.SetValue(null, server.DisplayName, RegistryValueKind.String);

                    //  Create the inproc key.
                    using (var inproc32Key = serverKey.CreateSubKey(KeyName_InProc32))
                    {
                        //  Check the key.
                        if(inproc32Key == null)
                            throw new InvalidOperationException("Cannot create InProc32 key.");

                        //  Set the .NET value.
                        inproc32Key.SetValue(null, KeyValue_NetFrameworkServer);

                        //  Create the values.
                        var assemblyVersion = server.GetType().Assembly.GetName().Version.ToString();
                        var assemblyFullName = server.GetType().Assembly.FullName;
                        var className = server.GetType().FullName;
                        var runtimeVersion = server.GetType().Assembly.ImageRuntimeVersion;
                        var codeBaseValue = server.GetType().Assembly.CodeBase;
                        const string threadingModel = "Both";

                        //  Register all details at server level.
                        inproc32Key.SetValue(KeyName_Assembly, assemblyFullName);
                        inproc32Key.SetValue(KeyName_Class, className);
                        inproc32Key.SetValue(KeyName_RuntimeVersion, runtimeVersion);
                        inproc32Key.SetValue(KeyName_ThreadingModel, threadingModel);
                        if (codeBase)
                            inproc32Key.SetValue(KeyName_CodeBase, codeBaseValue);

                        //  Create the version key.
                        using (var versionKey = inproc32Key.CreateSubKey(assemblyVersion))
                        {
                            //  Check the key.
                            if(versionKey == null)
                                throw new InvalidOperationException("Cannot create assembly version key.");

                            //  Set the values.
                            versionKey.SetValue(KeyName_Assembly, assemblyFullName);
                            versionKey.SetValue(KeyName_Class, className);
                            versionKey.SetValue(KeyName_RuntimeVersion, runtimeVersion);
                            if (codeBase)
                                versionKey.SetValue(KeyName_CodeBase, codeBaseValue);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Registers a SharpShell server. This will create the associations defined by the
 /// server's COMServerAssociation attribute.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 public static void RegisterServer(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Pass the server type to the SharpShellServer internal registration function and let it
     //  take over from there.
     SharpShellServer.DoRegister(server.GetType(), registrationType);
 }
Esempio n. 21
0
 /// <summary>
 /// Calls the method that starts the shell server.
 /// </summary>
 /// <param name="server"></param>
 public void StartShellServer(ISharpShellServer server)
 {
     Start(server);
 }
Esempio n. 22
0
        /// <summary>
        /// Installs a SharpShell COM server.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="registrationType">Type of the registration.</param>
        /// <param name="codeBase">if set to <c>true</c> use code base registration (i.e full assembly path, not the GAC).</param>
        public static void InstallServer(ISharpShellServer server, RegistrationType registrationType, bool codeBase)
        {
            //  Get the server registration information.
            var serverRegistrationInformation = GetServerRegistrationInfo(server, registrationType);

            //  If it is registered, unregister first.
            if (serverRegistrationInformation != null)
            {
                UninstallServer(server, registrationType);
            }

            //  Open the classes.
            using (var classesKey = OpenClassesKey(registrationType, RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                //  Create the server key.
                using (var serverKey = classesKey.CreateSubKey(server.ServerClsid.ToRegistryString()))
                {
                    if (serverKey == null)
                    {
                        throw new InvalidOperationException("Cannot create server key.");
                    }

                    //  We always set the server key default value to the display name if we can.
                    if (!string.IsNullOrEmpty(server.DisplayName))
                    {
                        serverKey.SetValue(null, server.DisplayName, RegistryValueKind.String);
                    }

                    //  Create the inproc key.
                    using (var inproc32Key = serverKey.CreateSubKey(KeyName_InProc32))
                    {
                        //  Check the key.
                        if (inproc32Key == null)
                        {
                            throw new InvalidOperationException("Cannot create InProc32 key.");
                        }

                        //  Set the .NET value.
                        inproc32Key.SetValue(null, KeyValue_NetFrameworkServer);

                        //  Create the values.
                        var          assemblyVersion  = server.GetType().Assembly.GetName().Version.ToString();
                        var          assemblyFullName = server.GetType().Assembly.FullName;
                        var          className        = server.GetType().FullName;
                        var          runtimeVersion   = server.GetType().Assembly.ImageRuntimeVersion;
                        var          codeBaseValue    = server.GetType().Assembly.CodeBase;
                        const string threadingModel   = "Both";

                        //  Register all details at server level.
                        inproc32Key.SetValue(KeyName_Assembly, assemblyFullName);
                        inproc32Key.SetValue(KeyName_Class, className);
                        inproc32Key.SetValue(KeyName_RuntimeVersion, runtimeVersion);
                        inproc32Key.SetValue(KeyName_ThreadingModel, threadingModel);
                        if (codeBase)
                        {
                            inproc32Key.SetValue(KeyName_CodeBase, codeBaseValue);
                        }

                        //  Create the version key.
                        using (var versionKey = inproc32Key.CreateSubKey(assemblyVersion))
                        {
                            //  Check the key.
                            if (versionKey == null)
                            {
                                throw new InvalidOperationException("Cannot create assembly version key.");
                            }

                            //  Set the values.
                            versionKey.SetValue(KeyName_Assembly, assemblyFullName);
                            versionKey.SetValue(KeyName_Class, className);
                            versionKey.SetValue(KeyName_RuntimeVersion, runtimeVersion);
                            if (codeBase)
                            {
                                versionKey.SetValue(KeyName_CodeBase, codeBaseValue);
                            }
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Registers a SharpShell server. This will create the associations defined by the
 /// server's COMServerAssociation attribute.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 public static void RegisterServer(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Pass the server type to the SharpShellServer internal registration function and let it 
     //  take over from there.
     SharpShellServer.DoRegister(server.GetType(), registrationType);
 }
Esempio n. 24
0
 /// <summary>
 /// Gets the server registration info.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 /// <returns>
 /// The ServerRegistrationInfo if the server is registered, otherwise false.
 /// </returns>
 public static ShellExtensionRegistrationInfo GetServerRegistrationInfo(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Call the main function.
     return(GetServerRegistrationInfo(server.ServerClsid, registrationType));
 }
 private void CheckIfRegisterOrUnregisterRequiresExplorerRestart(ISharpShellServer server)
 {
     if (server.ServerType == ServerType.ShellIconOverlayHandler)
     {
         if (MessageBox.Show(this, "This change will not take effect until Windows Explorer is restarted. Would you " +
                                   "like to restart Windows Explorer now?", "Restart Explorer?", MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question) == DialogResult.Yes)
         {
             ExplorerManager.RestartExplorer();
         }
     }
 }
 /// <summary>
 /// Gets the server registration info.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="registrationType">Type of the registration.</param>
 /// <returns>
 /// The ServerRegistrationInfo if the server is registered, otherwise false.
 /// </returns>
 public static ShellExtensionRegistrationInfo GetServerRegistrationInfo(ISharpShellServer server, RegistrationType registrationType)
 {
     //  Call the main function.
     return GetServerRegistrationInfo(server.ServerClsid, registrationType);
 }
Esempio n. 27
0
 /// <summary>
 /// Calls the method that stops the shell server.
 /// </summary>
 /// <param name="server"></param>
 public void StopShellServer(ISharpShellServer server)
 {
     Stop(server);
 }