private static void StopProtectHelper(Guid daemonProcessGuid) { string daemonProcessName = Assembly.GetExecutingAssembly().GetName().Name; Process daemonProcess = null; foreach (Process item in Process.GetProcessesByName(daemonProcessName)) { try { List <string> commandLineArguments = DaemonProcessHelper.GetCommandLineArguments(DaemonProcessHelper.GetCommandLineByProcessId(item.Id)); if (commandLineArguments != null && commandLineArguments.Count > 2 && commandLineArguments[0].Equals(daemonProcessGuid.ToString(), StringComparison.OrdinalIgnoreCase)) { daemonProcess = item; break; } } catch { } } if (daemonProcess != null) { try { daemonProcess.Kill(); } catch { } finally { daemonProcess.Dispose(); daemonProcess = null; } } }
public static void StartProtect(Guid daemonProcessGuid, int protectedProcessId, int delaySeconds, ProcessMode processMode, string entryPoint, params string[] args) { Thread thread = new Thread(() => { string daemonProcessFullPath = Assembly.GetExecutingAssembly().Location; string daemonProcessName = Assembly.GetExecutingAssembly().GetName().Name; List <string> daemonProcessArgs = new List <string>(); daemonProcessArgs.Add(string.Format("\"{0}\"", daemonProcessGuid.ToString())); daemonProcessArgs.Add(string.Format("\"{0}\"", protectedProcessId.ToString())); daemonProcessArgs.Add(string.Format("\"{0}\"", delaySeconds.ToString())); daemonProcessArgs.Add(string.Format("\"{0}\"", processMode.ToString())); daemonProcessArgs.Add(string.Format("\"{0}\"", entryPoint)); foreach (string item in args) { daemonProcessArgs.Add(string.Format("\"{0}\"", item)); } ProcessStartInfo startInfo = new ProcessStartInfo(daemonProcessFullPath); startInfo.Arguments = string.Join(" ", daemonProcessArgs.ToArray()); startInfo.CreateNoWindow = true; startInfo.ErrorDialog = false; startInfo.UseShellExecute = false; startInfo.Verb = "runas"; int exitCode = 0; Process daemonProcess = null; while (true) { if (daemonProcess != null) { if (!daemonProcess.HasExited) { daemonProcess.WaitForExit(); } try { exitCode = daemonProcess.ExitCode; } catch { } if (exitCode == -1) { break; } daemonProcess = null; } foreach (Process item in Process.GetProcessesByName(daemonProcessName)) { try { List <string> commandLineArguments = DaemonProcessHelper.GetCommandLineArguments(DaemonProcessHelper.GetCommandLineByProcessId(item.Id)); if (commandLineArguments != null && commandLineArguments.Count > 0 && commandLineArguments[0].Equals(daemonProcessGuid.ToString(), StringComparison.OrdinalIgnoreCase)) { daemonProcess = item; break; } } catch { } } if (daemonProcess == null) { try { daemonProcess = Process.Start(startInfo); } catch (Exception e) { InternalLogger.Log(e); break; } } } if (daemonProcess != null) { daemonProcess.Dispose(); daemonProcess = null; } }); thread.IsBackground = true; try { thread.Start(); } catch (Exception e) { InternalLogger.Log(e); } }
static void Main(string[] args) { //// args[0] = guid : daemon process guid //// args[1] = process id | -stop : protected process id | stop protecting //// args[2] = delay seconds : protected process delay start seconds //// args[3] = service | process : protected process mode //// args[4] = entry file | service name : protected process entry point //// args[5] = args : protected process args if (args == null || args.Length == 0) { InternalLogger.Log(new ArgumentNullException("args")); Console.WriteLine(@" args[0] = guid : daemon process guid args[1] = process id | -stop : protected process id | stop protecting args[2] = delay seconds : protected process delay start seconds args[3] = service | process : protected process mode args[4] = entry file | service name : protected process entry point args[5] = args : protected process args"); Environment.Exit(-1); } if (args.Length == 2) { string daemonProcessGuidString = args[0]; string daemonProcessStopString = args[1]; if (!daemonProcessStopString.Equals("-stop", StringComparison.OrdinalIgnoreCase)) { Environment.Exit(-1); } try { Guid daemonProcessGuid = new Guid(daemonProcessGuidString); Console.WriteLine("Stopping protecting {0}", daemonProcessGuidString); DaemonProcessManager.StopProtect(daemonProcessGuid); Console.WriteLine("Stopped protecting {0}", daemonProcessGuidString); Environment.Exit(-1); } catch (Exception e) { InternalLogger.Log(e); Environment.Exit(-1); } } if (args.Length < 4) { InternalLogger.Log(new ArgumentOutOfRangeException("args")); Environment.Exit(-1); } int protectedProcessId = -1; int.TryParse(args[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out protectedProcessId); int protectedProcessDelaySeconds = 5; try { int.TryParse(args[2], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out protectedProcessDelaySeconds); if (protectedProcessDelaySeconds < 0) { protectedProcessDelaySeconds = 0; } } catch (Exception e) { InternalLogger.Log(e); } string protectedProcessMode = args[3]; string protectedProcessEntryPoint = args[4]; string[] protectedProcessArgs = null; if (args.Length > 5) { protectedProcessArgs = new string[args.Length - 5]; Array.Copy(args, 5, protectedProcessArgs, 0, protectedProcessArgs.Length); } switch (protectedProcessMode.ToLowerInvariant()) { case "service": try { ServiceController[] services = ServiceController.GetServices(); bool isServiceExist = false; foreach (var item in services) { try { if (protectedProcessEntryPoint.Equals(item.ServiceName, StringComparison.OrdinalIgnoreCase)) { isServiceExist = true; break; } } catch (Exception e) { InternalLogger.Log(e); } } if (!isServiceExist) { Environment.Exit(-1); } } catch (Exception e) { InternalLogger.Log(e); Environment.Exit(-1); } ServiceController serviceController = null; try { serviceController = new ServiceController(protectedProcessEntryPoint); } catch (Exception e) { InternalLogger.Log(e); Environment.Exit(-1); } while (true) { try { serviceController.WaitForStatus(ServiceControllerStatus.Stopped); Thread.Sleep(TimeSpan.FromSeconds(protectedProcessDelaySeconds)); serviceController.Refresh(); if (serviceController.Status == ServiceControllerStatus.Stopped) { if (protectedProcessArgs == null || protectedProcessArgs.Length < 1) { serviceController.Start(); } else { serviceController.Start(protectedProcessArgs); } } } catch (Exception e) { InternalLogger.Log(e); } } case "process": if (protectedProcessArgs != null && protectedProcessArgs.Length > 0) { for (int i = 0; i < protectedProcessArgs.Length; i++) { protectedProcessArgs[i] = string.Format("\"{0}\"", protectedProcessArgs[i]); } } Process protectedProcess = null; if (protectedProcessId > 0) { try { protectedProcess = Process.GetProcessById(protectedProcessId); } catch (Exception e) { InternalLogger.Log(e); } } while (true) { if (protectedProcess != null && !protectedProcess.HasExited) { try { protectedProcess.WaitForExit(); Thread.Sleep(TimeSpan.FromSeconds(protectedProcessDelaySeconds)); protectedProcess = null; } catch (Exception e) { InternalLogger.Log(e); } } foreach (Process item in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(protectedProcessEntryPoint))) { try { if (item.MainModule.FileName.Equals(protectedProcessEntryPoint, StringComparison.OrdinalIgnoreCase)) { if (protectedProcessArgs == null || protectedProcessArgs.Length < 1) { protectedProcess = item; break; } else { List <string> commandLineArguments = DaemonProcessHelper.GetCommandLineArguments(DaemonProcessHelper.GetCommandLineByProcessId(item.Id)); if (DaemonProcessHelper.CommandLineArgumentsEquals(commandLineArguments, protectedProcessArgs, StringComparison.OrdinalIgnoreCase)) { protectedProcess = item; break; } } } } catch (Win32Exception) { } catch (Exception e) { InternalLogger.Log(e); } } if (protectedProcess == null) { try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = Path.GetFullPath(protectedProcessEntryPoint); startInfo.Arguments = (protectedProcessArgs == null || protectedProcessArgs.Length < 1) ? string.Empty : string.Join(" ", protectedProcessArgs); startInfo.Verb = "runas"; startInfo.WorkingDirectory = Path.GetDirectoryName(Path.GetFullPath(protectedProcessEntryPoint)); protectedProcess = Process.Start(startInfo); } catch (Exception e) { InternalLogger.Log(e); } } } default: Environment.Exit(-1); break; } }