Beispiel #1
0
 /// <summary>
 /// Starts a monitored UWP process using the applications family package name and app ID.
 /// </summary>
 /// <param name="startInfo"></param>
 /// <returns></returns>
 public static WardenProcess StartUwp(WardenStartInfo startInfo)
 {
     if (!Initialized)
     {
         throw new WardenManageException(Resources.Exception_Not_Initialized);
     }
     if (string.IsNullOrWhiteSpace(startInfo.PackageFamilyName))
     {
         throw new ArgumentException("packageFamilyName cannot be empty.");
     }
     //can be empty in some games
     if (string.IsNullOrWhiteSpace(startInfo.ApplicationId))
     {
         startInfo.ApplicationId = string.Empty;
     }
     if (CheckForWardenProcess(startInfo.FileName, out var existingProcess))
     {
         return(existingProcess);
     }
     if (string.IsNullOrWhiteSpace(startInfo.Arguments))
     {
         startInfo.Arguments = string.Empty;
     }
     return(UwpShell.LaunchApp(startInfo));
 }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        /// <summary>
        /// Launches an application URI via the Windows shell. Internally this method unwraps the URI
        /// and validates that it exist on the system.
        /// </summary>
        /// <param name="startInfo"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static WardenProcess StartUri(WardenStartInfo startInfo, Action <bool> callback)
        {
            if (!startInfo.ValidateUri)
            {
                return(StartByShell(startInfo, callback));
            }
            var(fileName, arguments, workingDirectory) = UriShell.ValidateUri(startInfo);
            var wardenInfo = new WardenStartInfo
            {
                FileName         = fileName,
                Arguments        = arguments,
                WorkingDirectory = workingDirectory,
                TargetFileName   = startInfo.TargetFileName,
                RaisePrivileges  = startInfo.RaisePrivileges,
                Filters          = startInfo.Filters,
                Track            = startInfo.Track
            };

            return(StartByShell(wardenInfo, callback));
        }
Beispiel #5
0
 /// <summary>
 /// Starts a monitored process using the applications full path.
 /// This method should only be used for win32 applications
 /// </summary>
 /// <param name="startInfo"></param>
 /// <returns></returns>
 public static WardenProcess Start(WardenStartInfo startInfo)
 {
     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.WorkingDirectory))
     {
         startInfo.WorkingDirectory = PathUtils.GetDirectoryName(startInfo.FileName);
     }
     if (CheckForWardenProcess(startInfo.FileName, out var existingProcess))
     {
         return(existingProcess);
     }
     if (string.IsNullOrWhiteSpace(startInfo.Arguments))
     {
         startInfo.Arguments = string.Empty;
     }
     return(UserShell.LaunchWin32App(startInfo));
 }