예제 #1
0
        private static WardenProcess BuildTreeById(int pId, List <ProcessFilter> filters, string processPath, List <string> commandLine)
        {
            try
            {
                Process process = null;
                try
                {
                    process = Process.GetProcessById(pId);
                }
                catch
                {
                    process = Process.GetProcesses().FirstOrDefault(it => it.Id == pId);
                }

                if (process == null)
                {
                    return(null);
                }
                var processName = process.ProcessName;
                var processId   = process.Id;
                var path        = processPath ?? GetProcessPath(processId);
                var state       = process.HasExited ? ProcessState.Dead : ProcessState.Alive;
                var arguments   = commandLine ?? GetCommandLine(processId);
                var warden      = new WardenProcess(processName, processId, path, state, arguments, filters);
                return(warden);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a WardenProcess from a process id and sets a parent.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parentId"></param>
        /// <param name="id"></param>
        /// <param name="filters">A list of filters so certain processes are not added to the tree.</param>
        /// <returns>A WardenProcess that will be added to a child list.</returns>
        internal static WardenProcess CreateProcessFromId(string name, int parentId, int id,
                                                          List <ProcessFilter> filters)
        {
            var           path      = GetProcessPath(id);
            var           arguments = GetCommandLine(id);
            WardenProcess warden;

            try
            {
                var process     = Process.GetProcessById(id);
                var processName = process.ProcessName;
                var processId   = process.Id;
                var state       = process.HasExited ? ProcessState.Dead : ProcessState.Alive;
                warden = new WardenProcess(processName, processId, path, state, arguments, ProcessTypes.Win32, filters);
                warden.SetParent(parentId);
                return(warden);
            }
            catch
            {
                //
            }
            warden = new WardenProcess(name, id, path, ProcessState.Dead, arguments, ProcessTypes.Win32, filters);
            warden.SetParent(parentId);
            return(warden);
        }
예제 #3
0
 private static WardenProcess BuildTreeById(int pId, List <ProcessFilter> filters)
 {
     try
     {
         Process process = null;
         try
         {
             process = Process.GetProcessById(pId);
         }
         catch
         {
             process = Process.GetProcesses().FirstOrDefault(it => it.Id == pId);
         }
         var processName = process.ProcessName;
         var processId   = process.Id;
         var path        = GetProcessPath(processId);
         var state       = process.HasExited ? ProcessState.Dead : ProcessState.Alive;
         var arguments   = GetCommandLine(processId);
         var type        = IsWindowsApp(path) ? ProcessTypes.Uwp : ProcessTypes.Win32;
         var warden      = new WardenProcess(processName, processId, path, state, arguments, type, filters);
         var children    = GetChildProcesses(pId);
         foreach (var child in children)
         {
             warden.AddChild(BuildTreeById(child.Id, filters));
         }
         return(new WardenProcess(processName, processId, path, state, arguments, type, filters));
     }
     catch (Exception)
     {
         return(null);
     }
 }
예제 #4
0
        /// <summary>
        /// Launches a system URI asynchronously and returns an empty Warden process set to Alive
        /// This spawns an asynchronous loop that will execute a callback if the target process is found
        /// However the function returns right away to ensure it does not block.
        /// </summary>
        /// <param name="uri">The URI that will be launched</param>
        /// <param name="path">The full path of the executable that should spawn after the URI launch.</param>
        /// <param name="arguments">Any additional arguments.</param>
        /// <param name="filters">A list of filters so certain processes are not added to the tree.</param>
        /// <param name="callback">A callback executed on if the process launched or not.</param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public static async Task <WardenProcess> StartUriAsync(string uri, string path, string arguments,
                                                               List <ProcessFilter> filters, Action <bool> callback, CancellationToken cancelToken, bool asUser = false, string workingDir = null)
        {
            if (!Initialized)
            {
                throw new WardenManageException(Resources.Exception_Not_Initialized);
            }
            if (CheckForWardenProcess(path, out var existingProcess))
            {
                return(existingProcess);
            }
            if (string.IsNullOrWhiteSpace(arguments))
            {
                arguments = string.Empty;
            }
            //lets add it to the dictionary ahead of time in case our program launches faster than we can return
            var key    = Guid.NewGuid();
            var warden = new WardenProcess(System.IO.Path.GetFileNameWithoutExtension(path),
                                           new Random().Next(1000000, 2000000), path, ProcessState.Alive, arguments?.SplitSpace(),
                                           ProcessTypes.Uri, filters)
            {
                FoundCallback = callback
            };

            ManagedProcesses[key] = warden;
            if (await new UriLauncher().PrepareUri(uri, path, arguments, cancelToken, key, asUser, workingDir) != null)
            {
                return(ManagedProcesses[key]);
            }
            ManagedProcesses.TryRemove(key, out var t);
            return(null);
        }
예제 #5
0
 /// <summary>
 /// Adds a child to a collection.
 /// </summary>
 /// <param name="child"></param>
 /// <returns></returns>
 internal bool AddChild(WardenProcess child)
 {
     if (child == null)
     {
         return(false);
     }
     if (Children == null)
     {
         Children = new ObservableCollection <WardenProcess>();
     }
     child.OnChildStateChange += OnChildOnStateChange;
     Children.Add(child);
     return(true);
 }
예제 #6
0
        /// <summary>
        /// Starts a process or URI via the Windows shell.
        /// </summary>
        /// <param name="startInfo"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static WardenProcess StartByShell(WardenStartInfo startInfo, Action <bool> callback = null)
        {
            if (!Initialized)
            {
                throw new WardenManageException(Resources.Exception_Not_Initialized);
            }
            if (string.IsNullOrWhiteSpace(startInfo.FileName))
            {
                throw new ArgumentException("fileName cannot be empty.");
            }
            if (startInfo.Track && string.IsNullOrWhiteSpace(startInfo.TargetFileName))
            {
                throw new ArgumentException("targetFileName cannot be empty.");
            }
            if (CheckForWardenProcess(startInfo.TargetFileName, out var existingProcess))
            {
                return(existingProcess);
            }
            if (string.IsNullOrWhiteSpace(startInfo.Arguments))
            {
                startInfo.Arguments = string.Empty;
            }

            var shellStartInfo = new ShellStartInfo(startInfo.FileName, startInfo.Arguments, startInfo.WorkingDirectory)
            {
                Verb = startInfo.RaisePrivileges ? "runas" : "open"
            };

            if (startInfo.Track)
            {
                var key  = Guid.NewGuid();
                var proc = new WardenProcess(System.IO.Path.GetFileNameWithoutExtension(startInfo.TargetFileName), GenerateProcessId(), startInfo.TargetFileName, ProcessState.Alive, startInfo.Arguments.SplitSpace(), startInfo.Filters)
                {
                    FoundCallback = callback
                };
                if (ManagedProcesses.TryAdd(key, proc))
                {
                    Api.StartByShell(shellStartInfo);
                    if (ManagedProcesses.TryGetValue(key, out var process))
                    {
                        return(process);
                    }
                }
            }
            Api.StartByShell(shellStartInfo);
            return(null);
        }
예제 #7
0
        /// <summary>
        /// Launches a system URI and returns an empty Warden process set to Alive
        /// This spawns an asynchronous loop that will execute a callback if the target process is found
        /// However the function returns right away to ensure it does not block.
        /// </summary>
        /// <param name="startInfo"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static WardenProcess StartUriDeferred(WardenStartInfo startInfo, Action <bool> callback)
        {
            if (!Initialized)
            {
                throw new WardenManageException(Resources.Exception_Not_Initialized);
            }
            if (string.IsNullOrWhiteSpace(startInfo.FileName))
            {
                throw new ArgumentException("fileName cannot be empty.");
            }
            if (string.IsNullOrWhiteSpace(startInfo.TargetFileName))
            {
                throw new ArgumentException("targetFileName cannot be empty.");
            }
            if (CheckForWardenProcess(startInfo.TargetFileName, out var existingProcess))
            {
                return(existingProcess);
            }
            if (string.IsNullOrWhiteSpace(startInfo.Arguments))
            {
                startInfo.Arguments = string.Empty;
            }

            //lets add it to the dictionary ahead of time in case our program launches faster than we can return
            var key    = Guid.NewGuid();
            var warden = new WardenProcess(System.IO.Path.GetFileNameWithoutExtension(startInfo.TargetFileName),
                                           GenerateProcessId(), startInfo.TargetFileName, ProcessState.Alive, startInfo.Arguments.SplitSpace(), startInfo.Filters)
            {
                FoundCallback = callback
            };

            if (ManagedProcesses.TryAdd(key, warden))
            {
                if (UriShell.LaunchUriDeferred(startInfo))
                {
                    if (ManagedProcesses.TryGetValue(key, out var process))
                    {
                        return(process);
                    }
                }
            }
            ManagedProcesses.TryRemove(key, out var failedProcess);
            return(null);
        }
예제 #8
0
        /// <summary>
        /// Checks if a process is currently running, if so we build the WardenProcess right away or fetch a monitored one.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="process"></param>
        /// <returns></returns>
        private static bool CheckForWardenProcess(string path, out WardenProcess process)
        {
            var runningProcess = GetProcess(path);

            if (runningProcess == null)
            {
                process = null;
                return(false);
            }
            //Lets check our watch list first
            foreach (var managedProcess in ManagedProcesses)
            {
                if (managedProcess.Value.Id != runningProcess.Id)
                {
                    continue;
                }
                process = ManagedProcesses[managedProcess.Key];
                return(true);
            }
            process = GetProcessFromId(runningProcess.Id);
            return(process != null);
        }
예제 #9
0
        /// <summary>
        /// Checks if a process is currently running, if so we build the WardenProcess right away or fetch a monitored one.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="process"></param>
        /// <returns></returns>
        private static bool CheckForWardenProcess(string path, out WardenProcess process)
        {
            using (var runningProcess = GetProcess(path))
            {
                if (runningProcess == null)
                {
                    process = null;
                    return(false);
                }

                foreach (var key in ManagedProcesses.Keys)
                {
                    if (ManagedProcesses.TryGetValue(key, out process))
                    {
                        if (process.Id == runningProcess.Id)
                        {
                            return(true);
                        }
                    }
                }
                process = null;
                return(false);
            }
        }
예제 #10
0
 private static WardenProcess BuildTreeById(int pId)
 {
     try
     {
         var process     = Process.GetProcessById(pId);
         var processName = process.ProcessName;
         var processId   = process.Id;
         var path        = ProcessUtils.GetProcessPath(processId);
         var state       = process.HasExited ? ProcessState.Dead : ProcessState.Alive;
         var arguments   = ProcessUtils.GetCommandLine(processId);
         var type        = ProcessUtils.IsWindowsApp(path) ? ProcessTypes.Uwp : ProcessTypes.Win32;
         var warden      = new WardenProcess(processName, processId, path, state, arguments, type);
         var children    = ProcessUtils.GetChildProcesses(pId);
         foreach (var child in children)
         {
             warden.AddChild(BuildTreeById(child.Id));
         }
         return(new WardenProcess(processName, processId, path, state, arguments, type));
     }
     catch (Exception)
     {
         return(null);
     }
 }