/// <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); } }
public static int Main(string[] args) { ms_appName = System.Reflection.Assembly.GetExecutingAssembly().Location; System.Text.UnicodeEncoding encode = new System.Text.UnicodeEncoding(); ms_appNameBytes = encode.GetBytes(ms_appName); ms_outputLogName = ApplicationName + OUTPUTEXT; try { ms_cmdLineArg.Parse(args); switch (ms_cmdLineArg.Action) { case Action.none: WriteToConsole("Command line arguments are required. Use /? or /help to see the usage."); break; // print the usage text case Action.help: break; // Install the NT Service case Action.install: NTService.Install(ms_cmdLineArg.NTServiceName, ms_cmdLineArg.UserName, ms_cmdLineArg.Password); break; // Uninstall the NT Service case Action.uninstall: NTService.Uninstall(ms_cmdLineArg.NTServiceName); break; // the program is being run and not installed/uninstalled case Action.run: //if the External Activation is started as a NT service if (ms_cmdLineArg.IsService) { NTService.MainFunction(ms_cmdLineArg.NTServiceName); } // if started from the Console or as an Application else { BootExternalActivator(); } break; default: throw new EAException("Unexpected action", Error.unexpectedError); } return(0); } catch (Exception e) { DoHardKill(e); return(0); } }