private void AutomationThread()
        {
            try
            {
                AutomatedUninstallManager.UninstallNsisQuietly(UninstallTarget, AutomatizeStatusCallback);

                OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Succeeded, Localization.Message_Success));
            }
            catch (AutomatedUninstallManager.AutomatedUninstallException ex)
            {
                Debug.Assert(ex.InnerException != null, "ex.InnerException != null");

                OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Failed, string.Format(Localization.Message_UninstallFailed, ex.InnerException?.Message ?? ex.Message)));

                // todo grace period / move to window?
                if (ex.UninstallerProcess != null && KillOnFail)
                {
                    try
                    {
                        ex.UninstallerProcess.Kill(true);
                    }
                    catch
                    {
                        // Ignore process errors, can't do anything about it
                    }
                }

                Program.ReturnValue = ReturnValue.FunctionFailedCode;
            }
        }
Esempio n. 2
0
        private static int Main(string[] args)
        {
            try { Console.OutputEncoding = Encoding.Unicode; }
            catch (IOException) { /*Old .NET v4 without support for unicode output*/ }

            if (args.Length < 2)
            {
                return(InvalidArgumentCode);
            }

            UninstallerType uType;

            if (!Enum.TryParse(args[0], out uType))
            {
                return(InvalidArgumentCode);
            }

            args = args.Skip(1).ToArray();

            if (args[0].Equals("/k", StringComparison.InvariantCultureIgnoreCase))
            {
                args        = args.Skip(1).ToArray();
                _killOnFail = true;
            }

            try
            {
                if (uType == UninstallerType.Nsis)
                {
                    var cline = string.Join(" ", args);
                    Console.WriteLine(@"Automatically uninstalling " + cline);
                    AutomatedUninstallManager.UninstallNsisQuietly(cline);
                    return(OkCode);
                }
            }
            catch (AutomatedUninstallManager.AutomatedUninstallException ex)
            {
                Console.WriteLine(@"Automatic uninstallation failed");
                Console.WriteLine(@"Reason: " + ex.InnerException.Message);

                if (ex.UninstallerProcess != null && _killOnFail)
                {
                    try
                    {
                        ex.UninstallerProcess.Kill(true);
                    }
                    catch
                    {
                        // Ignore process errors, can't do anything about it
                    }
                }

                return(FunctionFailedCode);
            }

            Console.WriteLine(uType + @" is not supported");
            return(InvalidArgumentCode);
        }
        /// <summary>
        /// Run in background as a daemon. Receive application PIDs to monitor, "stop" to exit.
        /// </summary>
        private void DaemonThread()
        {
            try
            {
                using (var server = new NamedPipeServerStream("UninstallAutomatizerDaemon", PipeDirection.In))
                    using (var reader = new StreamReader(server))
                    {
                        while (true)
                        {
                            server.WaitForConnection();
                            Debug.WriteLine("Client connected through pipe");
                            while (true)
                            {
                                var line = reader.ReadLine()?.ToLowerInvariant();

                                Debug.WriteLine("Received through pipe: " + (line ?? "NULL"));

                                if (line == null)
                                {
                                    Thread.Sleep(500);
                                    continue;
                                }

                                if (line == "stop")
                                {
                                    return;
                                }

                                int pid;
                                if (!int.TryParse(line, out pid))
                                {
                                    OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Normal,
                                                                                  string.Format(Localization.UninstallHandler_InvalidProcessNumber, pid)));
                                    continue;
                                }

                                try
                                {
                                    Task ttt;
                                    if (_runningHooks.TryGetValue(pid, out ttt) && !ttt.IsCompleted)
                                    {
                                        continue;
                                    }

                                    var target = Process.GetProcessById(pid);

                                    var app = Application.Attach(target);

                                    var t = new Task(() =>
                                    {
                                        try
                                        {
                                            Debug.WriteLine("Running automatizer on thread pool");
                                            AutomatedUninstallManager.AutomatizeApplication(app, AutomatizeStatusCallback);
                                        }
                                        catch (Exception ex)
                                        {
                                            OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Normal,
                                                                                          string.Format(Localization.Message_UninstallFailed,
                                                                                                        ex.InnerException?.Message ?? ex.Message)));
                                        }
                                        finally
                                        {
                                            Task tt;
                                            _runningHooks.TryRemove(pid, out tt);
                                        }
                                    });

                                    _runningHooks.AddOrUpdate(pid, t, (i, task) => t);

                                    Debug.WriteLine("Created automatizer thread");
                                    t.Start();
                                }
                                catch (SystemException ex) { Console.WriteLine(ex); }
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Normal,
                                                              Localization.UninstallHandler_DaemonStoppedReason + (ex.InnerException?.Message ?? ex.Message)));
            }
            finally
            {
                OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Succeeded, Localization.Message_Success));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Run in background as a daemon. Receive application PIDs to monitor, "stop" to exit.
        /// </summary>
        private void DaemonThread()
        {
            try
            {
                using (var server = new NamedPipeServerStream("UninstallAutomatizerDaemon"))
                    using (var reader = new StreamReader(server))
                    {
                        server.WaitForConnection();
                        while (true)
                        {
                            var line = reader.ReadLine()?.ToLowerInvariant();
                            if (line == null || line == "stop")
                            {
                                return;
                            }

                            int pid;
                            if (!int.TryParse(line, out pid))
                            {
                                throw new ArgumentException(pid + " is not a valid number");
                            }

                            try
                            {
                                lock (_runningHooks)
                                {
                                    if (_runningHooks.ContainsKey(pid) && !_runningHooks[pid].IsCompleted)
                                    {
                                        continue;
                                    }

                                    var target = Process.GetProcessById(pid);

                                    var app = Application.Attach(target);

                                    var t = Task.Factory.StartNew(() =>
                                    {
                                        try
                                        {
                                            AutomatedUninstallManager.AutomatizeApplication(app, AutomatizeStatusCallback);
                                        }
                                        catch (Exception ex)
                                        {
                                            OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Normal,
                                                                                          string.Format(Localization.Message_UninstallFailed,
                                                                                                        ex.InnerException?.Message ?? ex.Message)));
                                        }
                                        finally
                                        {
                                            lock (_runningHooks)
                                            {
                                                _runningHooks.Remove(pid);
                                            }
                                        }
                                    });

                                    _runningHooks.Add(pid, t);
                                }
                            }
                            catch (SystemException) { }
                        }
                    }
            }
            catch (Exception ex)
            {
                OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Normal,
                                                              string.Format("Daemon stopped because of: {0}", ex.InnerException?.Message ?? ex.Message)));
            }

            OnStatusUpdate(new UninstallHandlerUpdateArgs(UninstallHandlerUpdateKind.Succeeded, Localization.Message_Success));
        }