/// <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);
            }
        }
        /// <summary>
        /// Manages the commands when external activator is not running as service
        /// </summary>
        private static void DoCommandProcessing()
        {
            WriteToConsole("Commands");
            PrintCommandsHelp();
            WriteToConsole("Entering command mode.");

            while (true)
            {
                Console.Write("\nExternal Activator> ");
                string commandLine = Console.ReadLine();
                if (commandLine == null)
                {
                    continue;
                }

                commandLine = commandLine.ToLower().Trim();
                string command    = commandLine;
                string args       = "";
                int    commandEnd = commandLine.IndexOf(' ');
                if (commandEnd != -1)
                {
                    args    = commandLine.Substring(commandEnd).Trim();
                    command = commandLine.Substring(0, commandEnd);
                }

                //  commands without arguments
                if (args == "")
                {
                    if (command == "help" || command == "?" || command == "h")
                    {
                        PrintCommandsHelp();
                        continue;
                    }

                    if (command == "quit" || command == "q")
                    {
                        Quit();
                        break;
                    }

                    if (command == "status" || command == "s")
                    {
                        ReportApplicationStatus();
                        continue;
                    }

                    if (command == "config" || command == "c")
                    {
                        ReportConfigStatus();
                        continue;
                    }

                    if (command == "recycle" || command == "r")
                    {
                        RecycleLog();
                        continue;
                    }

                    if (command == "activator" || command == "a")
                    {
                        ReportServiceStatus();
                        continue;
                    }

                    if (command == "debug" || command == "d")
                    {
                        ReportDebugStatus();
                        continue;
                    }
                }
                // commands with arguments
                else
                {
                    if (command == "debug" || command == "d")
                    {
                        if (args == "on")
                        {
                            SetDebug(true);
                            continue;
                        }

                        if (args == "off")
                        {
                            SetDebug(false);
                            continue;
                        }
                    }
                }

                NTService.Execute(commandLine, 0);
                // WriteToConsole("Unknown command '" + commandLine + "' use 'help' for the list of valid commands");
            }
        }