Skip to content

Commit

Permalink
bug: srm uses regasm to register servers. Fixes #193.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwmkerr committed Oct 18, 2018
2 parents b686031 + 1c473e9 commit 23f96fc
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 205 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 2.4.1

- Fixed a bug where the Server Registration Manager could only register servers which were referencing the _same_ version of the SharpShell assembly as the manager itself (#194).

## Version 2.4

- Added `DesktopBackground` and `DirectoryBackground` association types.
Expand Down
4 changes: 2 additions & 2 deletions SharpShell/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.2.0")]
[assembly: AssemblyFileVersion("2.4.2.0")]
119 changes: 26 additions & 93 deletions SharpShell/Tools/ServerRegistrationManager/Application.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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));
Expand Down Expand Up @@ -97,41 +92,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool
return;
}

// Try and load the server types.
IEnumerable<ISharpShellServer> 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);
}
}

Expand All @@ -142,42 +114,18 @@ private void InstallServer(string path, RegistrationType registrationType, bool
/// <param name="registrationType">Type of the registration.</param>
private void UninstallServer(string path, RegistrationType registrationType)
{
// Try and load the server types.
IEnumerable<ISharpShellServer> 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);
}
}

Expand All @@ -192,22 +140,7 @@ private void ShowWelcome()
outputService.WriteMessage("========================================");
outputService.WriteMessage("");
}

/// <summary>
/// Loads the server types from an assembly.
/// </summary>
/// <param name="assemblyPath">The assembly path.</param>
/// <returns>The SharpShell Server types from the assembly.</returns>
private static IEnumerable<ISharpShellServer> 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<ISharpShellServer>().Select(st => st.Value);
}


private const string VerbHelp = @"help";
private const string VerbInstall = @"install";
private const string VerbUninstall = @"uninstall";
Expand All @@ -216,7 +149,7 @@ private static IEnumerable<ISharpShellServer> 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";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SharpShell.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -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);
}

/// <summary>
/// Writes the success.
/// </summary>
/// <param name="error">The error.</param>
/// <param name="messabe">The message.</param>
/// <param name="log">if set to <c>true</c> [log].</param>
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);
}

/// <summary>
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion SharpShell/Tools/ServerRegistrationManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ServerRegistrationManager
{
class Program
internal class Program
{
static void Main(string[] args)
{
Expand Down
Loading

0 comments on commit 23f96fc

Please sign in to comment.