示例#1
0
            public static Process ResolveParent(int processId)
            {
                Process      process = Process.GetProcessById(processId);
                ProcessUtils pbi     = new ProcessUtils();
                int          result  = NtQueryInformationProcess(process.Handle, 0, ref pbi, Marshal.SizeOf(pbi), out var returnLength);

                if (result != 0)
                {
                    throw new Win32Exception(result);
                }

                try
                {
                    return(Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32()));
                }
                catch (ArgumentException)
                {
                    return(null);
                }
            }
示例#2
0
 private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ProcessUtils processInformation, int processInformationLength, out int returnLength);
示例#3
0
        private static void SpawnConhostProcess(bool mute)
        {
            Logger.Instance.Debug($"SpawnConhostProcess starts mute={mute}");

            Process cmdProcess = new Process();

            cmdProcess.StartInfo.FileName    = "cmd.exe";
            cmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            cmdProcess.StartInfo.Arguments   = "/k timeout 1 && \x07";
            if (cmdProcess.Start() == false)
            {
                Logger.Instance.Debug($"Can't start {cmdProcess.StartInfo.FileName} {cmdProcess.StartInfo.Arguments}");
                return;
            }

            const int sTimeout         = 3;
            int       conhostProcessId = 0;

            Calculate(() =>
            {
                if (!cmdProcess.HasExited)
                {
                    foreach (Process conhost in Process.GetProcessesByName("conhost"))
                    {
                        try
                        {
                            Process parent = ProcessUtils.ResolveParent(conhost.Id);
                            if (parent != null && cmdProcess.Id == parent.Id)
                            {
                                conhostProcessId = conhost.Id;
                                return(true);
                            }
                        }
                        catch (Win32Exception e)
                        {
                            Logger.Instance.Debug($"Exception with message \"{e.Message}\" on " +
                                                  $"getting process parent id for conhost process {conhost.Id}");
                        }
                    }
                }
                return(false);
            }, new TimeSpan(0, 0, sTimeout));

            if (conhostProcessId == 0)
            {
                Logger.Instance.Debug($"Can't find child conhost process");
                return;
            }

            Logger.Instance.Debug($"Spawned conhost process id={conhostProcessId}. Starting muting of generated bell sound.");

            Calculate(() =>
            {
                return(MuteProcess(conhostProcessId, true, true) != null);
            }, new TimeSpan(0, 0, sTimeout));

            cmdProcess.WaitForExit(2000);

            Logger.Instance.Debug($"Set conhost process mute state to {mute}.");

            MuteProcess(conhostProcessId, mute);

            cmdProcess.Kill();

            Logger.Instance.Debug($"SpawnConhostProcess finishes mute={mute}");
        }