From 1c473e9e21640ccbd8ab34dada3609984b9922bd Mon Sep 17 00:00:00 2001 From: Dave Kerr Date: Thu, 18 Oct 2018 22:45:33 +0900 Subject: [PATCH] bug: srm uses regasm to register servers. Fixes #193. --- SharpShell/SharedAssemblyInfo.cs | 4 +- .../ServerRegistrationManager/Application.cs | 119 +++------- .../OutputService/ConsoleOutputService.cs | 25 ++- .../ServerRegistrationManager/Program.cs | 2 +- .../ServerRegistrationManager.csproj | 205 +++++++++--------- 5 files changed, 150 insertions(+), 205 deletions(-) diff --git a/SharpShell/SharedAssemblyInfo.cs b/SharpShell/SharedAssemblyInfo.cs index 44ac7309..5385e314 100644 --- a/SharpShell/SharedAssemblyInfo.cs +++ b/SharpShell/SharedAssemblyInfo.cs @@ -17,5 +17,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.4.0.0")] -[assembly: AssemblyFileVersion("2.4.0.0")] +[assembly: AssemblyVersion("2.4.1.0")] +[assembly: AssemblyFileVersion("2.4.1.0")] diff --git a/SharpShell/Tools/ServerRegistrationManager/Application.cs b/SharpShell/Tools/ServerRegistrationManager/Application.cs index 2aaec113..8ab725ec 100644 --- a/SharpShell/Tools/ServerRegistrationManager/Application.cs +++ b/SharpShell/Tools/ServerRegistrationManager/Application.cs @@ -1,14 +1,10 @@ using System; -using System.Collections.Generic; -using System.ComponentModel.Composition.Hosting; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; using ServerRegistrationManager.Actions; using ServerRegistrationManager.OutputService; -using SharpShell; using SharpShell.Diagnostics; +using SharpShell.Helpers; using SharpShell.ServerRegistration; namespace ServerRegistrationManager @@ -55,8 +51,8 @@ public void Run(string[] args) // Get the verb, target and parameters. var verb = args[0]; - var target = args.Length > 1 ? args[1] : (string)null; // TODO tidy this up. - var parameters = args.Skip(1); + var target = args.Length > 1 ? args[1] : null; // TODO tidy this up. + var parameters = args.Skip(1).ToArray(); //Allow user to override registrationType with -os32 or -os64 if (parameters.Any(p => p.Equals(ParameterOS32, StringComparison.InvariantCultureIgnoreCase))) @@ -68,7 +64,6 @@ public void Run(string[] args) registrationType = RegistrationType.OS64Bit; } - // Based on the verb, perform the action. if (verb == VerbInstall) InstallServer(target, registrationType, parameters.Any(p => p == ParameterCodebase)); @@ -97,41 +92,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool return; } - // Try and load the server types. - IEnumerable serverTypes = null; - try - { - serverTypes = LoadServerTypes(path); - } - catch (Exception e) - { - outputService.WriteError("An unhandled exception occured when loading the SharpShell"); - outputService.WriteError("Server Types from the specified assembly. Is it a SharpShell"); - outputService.WriteError("Server Assembly?"); - System.Diagnostics.Trace.Write(e); - Logging.Error("An unhandled exception occured when loading a SharpShell server.", e); - } + var regasm = new RegAsm(); + var success = registrationType == RegistrationType.OS32Bit ? regasm.Register32(path, codeBase) : regasm.Register64(path, codeBase); - // Install each server type. - foreach (var serverType in serverTypes) + if (success) { - // Inform the user we're going to install the server. - outputService.WriteMessage("Preparing to install (" + registrationType + "): " + serverType.DisplayName, true); - - // Install the server. - try - { - SharpShell.ServerRegistration.ServerRegistrationManager.InstallServer(serverType, registrationType, codeBase); - SharpShell.ServerRegistration.ServerRegistrationManager.RegisterServer(serverType, registrationType); - } - catch (Exception e) - { - outputService.WriteError("Failed to install and register the server."); - Logging.Error("An unhandled exception occured installing and registering the server " + serverType.DisplayName, e); - continue; - } - - outputService.WriteSuccess(" " + serverType.DisplayName + " installed and registered.", true); + outputService.WriteSuccess($" {path} installed and registered.", true); + outputService.WriteMessage(regasm.StandardOutput); + } + else + { + outputService.WriteError($" {path} failed to register.", true); + outputService.WriteError(regasm.StandardError); } } @@ -142,42 +114,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool /// Type of the registration. private void UninstallServer(string path, RegistrationType registrationType) { - // Try and load the server types. - IEnumerable serverTypes = null; - try - { - serverTypes = LoadServerTypes(path); - } - catch (Exception e) - { - outputService.WriteError("An unhandled exception occured when loading the SharpShell"); - outputService.WriteError("Server Types from the specified assembly. Is it a SharpShell"); - outputService.WriteError("Server Assembly?"); - System.Diagnostics.Trace.Write(e); - Logging.Error("An unhandled exception occured when loading a SharpShell server.", e); - } + var regasm = new RegAsm(); + var success = registrationType == RegistrationType.OS32Bit ? regasm.Unregister32(path) : regasm.Unregister64(path); - // Install each server type. - foreach (var serverType in serverTypes) + if (success) + { + outputService.WriteSuccess($" {path} uninstalled.", true); + outputService.WriteMessage(regasm.StandardOutput); + } + else { - // Inform the user we're going to install the server. - Console.WriteLine("Preparing to uninstall (" + registrationType + "): " + serverType.DisplayName, true); - - // Install the server. - try - { - - SharpShell.ServerRegistration.ServerRegistrationManager.UnregisterServer(serverType, registrationType); - SharpShell.ServerRegistration.ServerRegistrationManager.UninstallServer(serverType, registrationType); - } - catch (Exception e) - { - outputService.WriteError("Failed to unregister and uninstall the server."); - Logging.Error("An unhandled exception occured un registering and uninstalling the server " + serverType.DisplayName, e); - continue; - } - - outputService.WriteSuccess(" " + serverType.DisplayName + " unregistered and uninstalled.", true); + outputService.WriteError($" {path} failed to uninstall.", true); + outputService.WriteError(regasm.StandardError); } } @@ -192,22 +140,7 @@ private void ShowWelcome() outputService.WriteMessage("========================================"); outputService.WriteMessage(""); } - - /// - /// Loads the server types from an assembly. - /// - /// The assembly path. - /// The SharpShell Server types from the assembly. - private static IEnumerable LoadServerTypes(string assemblyPath) - { - // Create an assembly catalog for the assembly and a container from it. - var catalog = new AssemblyCatalog(Path.GetFullPath(assemblyPath)); - var container = new CompositionContainer(catalog); - - // Get all exports of type ISharpShellServer. - return container.GetExports().Select(st => st.Value); - } - + private const string VerbHelp = @"help"; private const string VerbInstall = @"install"; private const string VerbUninstall = @"uninstall"; @@ -216,7 +149,7 @@ private static IEnumerable LoadServerTypes(string assemblyPat private const string ParameterCodebase = @"-codebase"; - private const String ParameterOS32 = @"-os32"; - private const String ParameterOS64 = @"-os64"; + private const string ParameterOS32 = @"-os32"; + private const string ParameterOS64 = @"-os64"; } } diff --git a/SharpShell/Tools/ServerRegistrationManager/OutputService/ConsoleOutputService.cs b/SharpShell/Tools/ServerRegistrationManager/OutputService/ConsoleOutputService.cs index 3754110c..df2f6eae 100644 --- a/SharpShell/Tools/ServerRegistrationManager/OutputService/ConsoleOutputService.cs +++ b/SharpShell/Tools/ServerRegistrationManager/OutputService/ConsoleOutputService.cs @@ -1,4 +1,5 @@ -using System; +using SharpShell.Diagnostics; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -24,21 +25,29 @@ public void WriteMessage(string message, bool log = false) // Write the message. Console.WriteLine(message); + + // If required, write to the SharpShell log. + if (log) + Logging.Log(message); } /// /// Writes the success. /// - /// The error. + /// The message. /// if set to true [log]. - public void WriteSuccess(string error, bool log = false) + public void WriteSuccess(string message, bool log = false) { // Set the colour. Console.ForegroundColor = ConsoleColor.Green; // Write the message. - Console.WriteLine(error); - Console.ForegroundColor = ConsoleColor.Gray; + Console.WriteLine(message); + Console.ForegroundColor = ConsoleColor.Gray; + + // If required, write to the SharpShell log. + if (log) + Logging.Log(message); } /// @@ -53,7 +62,11 @@ public void WriteError(string error, bool log = false) // Write the message. Console.WriteLine(error); - Console.ForegroundColor = ConsoleColor.Gray; + Console.ForegroundColor = ConsoleColor.Gray; + + // If required, write to the SharpShell log. + if (log) + Logging.Error(error); } } } diff --git a/SharpShell/Tools/ServerRegistrationManager/Program.cs b/SharpShell/Tools/ServerRegistrationManager/Program.cs index 653d13ab..36dc90fe 100644 --- a/SharpShell/Tools/ServerRegistrationManager/Program.cs +++ b/SharpShell/Tools/ServerRegistrationManager/Program.cs @@ -8,7 +8,7 @@ namespace ServerRegistrationManager { - class Program + internal class Program { static void Main(string[] args) { diff --git a/SharpShell/Tools/ServerRegistrationManager/ServerRegistrationManager.csproj b/SharpShell/Tools/ServerRegistrationManager/ServerRegistrationManager.csproj index 58b79489..a56af8a9 100644 --- a/SharpShell/Tools/ServerRegistrationManager/ServerRegistrationManager.csproj +++ b/SharpShell/Tools/ServerRegistrationManager/ServerRegistrationManager.csproj @@ -1,110 +1,109 @@ - - - - - Debug - AnyCPU - {80B7B105-FDEE-44D7-B2A3-5DD61A80C93B} - Exe - Properties - ServerRegistrationManager - ServerRegistrationManager - v4.5 - 512 - - - - - - - - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - true - - - Key.snk - - - OnOutputUpdated - - - app.manifest - - - - - - - - - - - - - - Properties\SharedAssemblyInfo.cs - - - - - - - - - - - - - - Designer - - - - - - {b5d5f670-bfba-4d09-91c6-74bb12b7edd4} - SharpShell - - - - - - - - - - - copy "$(TargetPath)" "$(SolutionDir)..\Build\Tools\srm.exe" - - - copy "$(SolutionDir)..\Build\Core\SharpShell.dll" "$(ProjectDir)EmbeddedReferences\SharpShell.dll" - + + + + + Debug + AnyCPU + {80B7B105-FDEE-44D7-B2A3-5DD61A80C93B} + Exe + Properties + ServerRegistrationManager + ServerRegistrationManager + v4.5 + 512 + + + + + + + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + + + Key.snk + + + OnOutputUpdated + + + app.manifest + + + + + + + + + + + + + Properties\SharedAssemblyInfo.cs + + + + + + + + + + + + + + Designer + + + + + + {b5d5f670-bfba-4d09-91c6-74bb12b7edd4} + SharpShell + + + + + + + + + + + copy "$(TargetPath)" "$(SolutionDir)..\Build\Tools\srm.exe" + + + copy "$(SolutionDir)..\Build\Core\SharpShell.dll" "$(ProjectDir)EmbeddedReferences\SharpShell.dll" + + --> \ No newline at end of file