2

我使用这个简单的代码在控制台上列出所有正在运行的进程及其体系结构(32bit或),它的工作原理几乎准确,但结果中的进程数甚至不是我在WindowsTaskManagerProcessHacker64bit中看到的一半。我的大部分运行进程都不包含在返回结果中。它几乎只返回系统进程。有时我得到“ ”或“拒绝访问”(可能是因为某些进程受到保护),有时没有例外:Win32Exception

internal static class Program
{
    private static void Main()
    {
        foreach (var p in Process.GetProcesses())
        {
            try
            {
                if (p.IsWin64Emulator())
                {
                    Console.WriteLine(p.ProcessName + " x86 " + p.MainModule.FileName);
                }
                else
                {
                    Console.WriteLine(p.ProcessName + " x64 " + p.MainModule.FileName);
                }
            }
            catch (Win32Exception ex)
            {
                if (ex.NativeErrorCode != 0x00000005)
                {
                    throw;
                }
            }
        }

        Console.ReadLine();
    }

    private static bool IsWin64Emulator(this Process process)
    {
        if (Environment.OSVersion.Version.Major > 5 || Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1)
        {
            return NativeMethods.IsWow64Process(process.Handle, out var retVal) && retVal;
        }

        return false;
    }
}

internal static class NativeMethods
{
    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
}

所以这是我的问题:为什么这段代码没有显示结果中的所有过程?以及如何解决这个问题?

包含的示例:

conhost x64
OpenWith x64
LockApp x64
ShellExperienceHost x64
SearchUI x64
...

不包括的示例:

CSh_Test x64 --> Current Running Sotfware in debug mode
explorer x64
ccSvcHst x86 --> Symantec AV
devenv x86   --> Visual Studio
XYplorer x86 --> File manager
4

1 回答 1

1

GetProcesses() 为您提供正在运行的进程,但不提供也在 WindowsTaskManager 中显示的底层服务。也许这就是你正在经历的差异?

来源:https ://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getprocesses?view=netframework-4.7.2

如果没有,请提供更多关于显示什么和不显示什么的信息。

于 2019-01-30T13:00:56.047 回答