我使用这个简单的代码在控制台上列出了所有正在运行的进程及其架构(32bit
或64bit
),它的工作原理几乎是准确的,但是结果的进程数甚至不到一半我在 WindowsTaskManager 或 ProcessHacker 中看到的内容。我的大多数正在运行的进程都不包含在返回结果中。它几乎只返回系统进程。有时我会收到“ {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
答案 0 :(得分:0)
GetProcesses()为您提供正在运行的进程,但不提供WindowsTaskManager中也显示的基础服务。也许这就是您所经历的不同?
如果没有,请提供更多有关所显示内容和未显示内容的信息。