/// <summary> // Installs the Windows NT Service. Uses a transacted installer, so that // if installation fails midstream for some reason, installation is rolled // back. Post installation, the registry settings for the service are modified // to change the command-line parameters (because currently, CLR does not allow it) // If registry modification fails for some reason, uninstall is called on the service. /// </summary> /// <param name="svcname">Service to install</param> /// <param name="username">User name to execute under</param> /// <param name="password">Password of the user</param> public static void Install( string svcname, string username, string password) { bool doUninstall = false; try { Global.WriteStatus("External Activator is installing as NT service '" + svcname + "' ..."); TransactedInstaller ti = new TransactedInstaller(); // Sets information required for installing (might get username and password information from the user) ProjectInstaller mi = new ProjectInstaller(svcname, username, password); ti.Installers.Add(mi); String path = String.Format("/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location); String[] cmdline = { path }; InstallContext ctx = new InstallContext(String.Format(svcname + ".InstallLog"), cmdline); ti.Context = ctx; // Starts the installation process ti.Install(new Hashtable()); doUninstall = true; // Registry modification of Command-Line arguments to include (-svc) followed by the service name // to install it as a particular External Activator. RegistryKey key = Registry.LocalMachine.OpenSubKey(String.Format("System\\CurrentControlSet\\Services\\{0}", svcname), true); object ImagePathObject = key.GetValue("ImagePath"); if (ImagePathObject != null) { Global.WriteDebugInfo("Modifying the registry to include command-line parameters after installation."); string ImagePath = String.Format("{0} /{1}:{2}", (string)ImagePathObject, CommandLineArgument.RunAsNTServiceArgument, svcname); key.SetValue("ImagePath", ImagePath); } else { // Calls Uninstall if registry modification not possible throw new EAException("Critical Error : Could not open ImagePath key of NT service '" + svcname + "' in the registry. Uninstalling service", Error.postInstallProblem); } Global.WriteStatus("External Activator is successfully installed as NT service '" + svcname + "' ..."); } catch (Exception e) { if (doUninstall) { try { NTService.Uninstall(svcname); } catch (Exception eNested) { EAException.Report(eNested); } } throw new EAException("External Activation Installation failed", Error.installProblem, e); } }
/// <summary> // Uninstalls the Windows NT Service. Uses a transacted installer, so that // if installation fails midstream for some reason, installation is rolled // back. /// </summary> /// <param name="svcname">Service to uninstall</param> public static void Uninstall(string svcname) { try { Global.WriteStatus("External Activator is uninstalling service " + svcname + "..."); TransactedInstaller ti = new TransactedInstaller(); ProjectInstaller mi = new ProjectInstaller(svcname, null, null); ti.Installers.Add(mi); String path = String.Format("/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location); String[] cmdline = { path }; InstallContext ctx = new InstallContext(String.Format(svcname + ".InstallLog"), cmdline); ti.Context = ctx; ti.Uninstall(null); Global.WriteStatus("External Activator service '" + svcname + "' uninstaled successfully."); } catch (Exception e) { throw new EAException("External Activator service '" + svcname + "' uninstalling failed!", Error.unexpectedError, e); } }