예제 #1
0
        private void OnDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                BackgroundWorker worker = sender as BackgroundWorker;

                FormsHelper helper = e.Argument as FormsHelper;
                if (helper == null)
                {
                    e.Result = -1;

                    return;
                }

                RegistrationOptions options = helper.Options;
                if (options == null)
                {
                    e.Result = -1;

                    return;
                }

                RegistrationHandler register = new RegistrationHandler();

                e.Result = register.Run(options, helper);
            }
            catch
            {
                e.Result = -1;

                return;
            }
        }
예제 #2
0
        public void SetOptions(RegistrationOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options",
                                                "The options parameter cannot be null (or Nothing).");
            }

            _cmdOptions = options;
        }
예제 #3
0
        static int Main(string[] args)
        {
            // 1. Parse the commandline options to determine the requested task...
            RegistrationOptions options = new RegistrationOptions();
            bool argValid = options.Parse(args);

            // 2. Get a pointer to the forground window.  The idea here is that
            // If the user is starting our application from an existing console
            // shell, that shell will be the uppermost window.  We'll get it
            // and attach to it.
            // Uses this idea from, Jeffrey Knight, since it fits our model instead
            // of the recommended ATTACH_PARENT_PROCESS (DWORD)-1 parameter
            bool   startedInConsole = false;
            IntPtr ptr       = GetForegroundWindow();
            int    processId = -1;

            GetWindowThreadProcessId(ptr, out processId);
            Process process = Process.GetProcessById(processId);

            startedInConsole = process != null && String.Equals(process.ProcessName,
                                                                "cmd", StringComparison.InvariantCultureIgnoreCase);

            // 3. If the command option is invalid, or we just needed to show help...
            int returnValue = -1;

            if (!argValid || options.ShowHelp)
            {
                IRegistrationHelper helper = null;

                bool consoleSuccess = false;
                if (startedInConsole)
                {
                    consoleSuccess = CreateConsole(startedInConsole, process);
                    helper         = new ConsoleHelper();
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    helper = new FormsHelper();
                }
                if (helper != null)
                {
                    helper.DisplayHelp();
                }
                if (consoleSuccess)
                {
                    FreeConsole();
                }

                return(returnValue);
            }

            // 4. If requested to open the help file, process it...
            if (options.IsViewer)
            {
                bool consoleSuccess        = false;
                IRegistrationHelper helper = null;

                try
                {
                    if (startedInConsole)
                    {
                        consoleSuccess = CreateConsole(startedInConsole, process);
                        helper         = new ConsoleHelper();
                    }
                    else
                    {
                        helper = new NoneHelper();
                    }

                    RegistrationHandler register = new RegistrationHandler();

                    returnValue = register.Run(options, helper);
                }
                catch (Exception ex)
                {
                    if (startedInConsole)
                    {
                        if (helper != null)
                        {
                            helper.WriteLine(ex);
                        }
                    }
                    else
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                if (consoleSuccess)
                {
                    FreeConsole();
                }

                return(returnValue);
            }

            // If started from the console, we will ignore any GUI request...
            if (startedInConsole && !options.IsConsole)
            {
                options.SetMode(startedInConsole);
            }

            if (options.IsConsole)
            {
                bool consoleSuccess = false;
                try
                {
                    consoleSuccess = CreateConsole(startedInConsole, process);

                    if (consoleSuccess)
                    {
                        ConsoleHelper helper = new ConsoleHelper();

                        RegistrationHandler register = new RegistrationHandler();

                        returnValue = register.Run(options, helper);
                    }
                }
                catch
                {
                    FormsHelper helper = new FormsHelper();
                    helper.DisplayHelp();
                }
                finally
                {
                    if (consoleSuccess)
                    {
                        FreeConsole();
                    }
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                MainForm mainForm = new MainForm();

                mainForm.SetOptions(options);

                Application.Run(mainForm);

                returnValue = mainForm.Result;
            }

            return(returnValue);
        }
예제 #4
0
        public int Run(RegistrationOptions options, IRegistrationHelper helper)
        {
            if (options == null || helper == null)
            {
                return(-1);
            }

            _regOptions = options;
            _regHelper  = helper;

            RegistrationHelper.Instance.Add(_regHelper);

            if (options.IsViewer)
            {
                this.DoViewerStuff();

                return(0);
            }

            bool isErrorCodes = _regOptions.ErrorCodes;

            if (_regOptions.ShowLogo)
            {
                _regHelper.DisplayLogo();
            }
            if (_regOptions.ShowHelp || _regOptions.Count == 0)
            {
                _regHelper.DisplayHelp();

                return(isErrorCodes ? 1 : -1);
            }

            if (!ApplicationHelpers.IsClassRegistered(
                    "{31411198-A502-11D2-BBCA-00C04F8EC294}"))
            {
                if (!_regOptions.IsQuiet)
                {
                    _regHelper.WriteLine(String.Format(ResourcesHelper.GetString(
                                                           "ErrorNoHelp2Environment")));
                }

                return(isErrorCodes ? 2 : -1);
            }

            if (!ApplicationHelpers.IsThisUserPrivileged())
            {
                if (!_regOptions.IsQuiet)
                {
                    _regHelper.WriteLine(String.Format(ResourcesHelper.GetString(
                                                           "ErrorInvalidPrivileges")));
                }

                return(isErrorCodes ? 3 : -1);
            }

            string actionParam = _regOptions.ActionParam;

            if (actionParam != "/r" && actionParam != "/u" && actionParam != "+r" &&
                actionParam != "-r" && actionParam != "+p" && actionParam != "-p" &&
                actionParam != "/v" && actionParam != "-v")
            {
                if (!_regOptions.IsQuiet)
                {
                    _regHelper.WriteLine(String.Format(ResourcesHelper.GetString(
                                                           "ErrorInvalidCommandLine"), actionParam));
                }

                return(isErrorCodes ? 4 : -1);
            }

            if (String.IsNullOrEmpty(_regOptions.FileName) ||
                !File.Exists(_regOptions.FileName))
            {
                if (!_regOptions.IsQuiet)
                {
                    _regHelper.WriteLine(String.Format(ResourcesHelper.GetString(
                                                           "ErrorInvalidXmlFile"), _regOptions.FileName));
                }

                return(isErrorCodes ? 5 : -1);
            }

            XmlValidator schemaValidator = new XmlValidator(helper);

            if (!schemaValidator.SchemaExists)
            {
                if (!_regOptions.IsQuiet)
                {
                    _regHelper.WriteLine(String.Format(ResourcesHelper.GetString(
                                                           "ErrorCannotValidateXmlFile"), _regOptions.FileName));
                }

                return(isErrorCodes ? 6 : -1);
            }

            if (!schemaValidator.Validate(_regOptions.FileName, _regOptions.IsQuiet))
            {
                // get a message from the validator class
                return(isErrorCodes ? 6 : -1);
            }

            if (actionParam != "/v" && actionParam != "-v")
            {
                _regHelper.CloseViewers();
            }

            if (actionParam == "/r" || actionParam == "+r")
            {
                this.DoHelpStuff(true);
            }
            if (actionParam == "/r" || actionParam == "+p")
            {
                this.DoPluginStuff(true);
            }
            if (actionParam == "/u" || actionParam == "-p")
            {
                this.DoPluginStuff(false);
            }
            if (actionParam == "/u" || actionParam == "-r")
            {
                this.DoHelpStuff(false);
            }
            if (actionParam == "/v" || actionParam == "-v" ||
                _regOptions.ViewHelp)
            {
                this.DoViewerStuff();
            }

            return(0);
        }
예제 #5
0
 public FormsHelper(MainForm mainform, RegistrationOptions options)
 {
     _mainForm = mainform;
     _options  = options;
 }