获取C#中所有进程的句柄 [英] Getting handles of all processes in C#

查看:193
本文介绍了获取C#中所有进程的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我写了这个循环来通过使用Process.GetProcesses()来获取所有进程。



Hello, i wrote this loop to get all processes by using Process.GetProcesses()

foreach (var currentProcess in Process.GetProcesses())
{

    bool is32bit = false;
    IntPtr hprocess = OpenProcess((PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_WRITE), false, currentProcess.Id);
    IsWow64Process(hprocess, out is32bit);

    if (hprocess != IntPtr.Zero)
    {
        if (is32bit = false)
        {
            Console.WriteLine("{0}: {1} - 64 BIT", hprocess, currentProcess.ProcessName);
        }
        else
        {
            Console.WriteLine("{0}: {1} - 32 BIT", hprocess, currentProcess.ProcessName);
        }
    }


    CloseHandle(hprocess);
}





我正面临一些问题...



1)我在许多进程中得到0而不是句柄IntPtr

2)在那里我没有得到0,我得到多个进程的相同句柄(下面的示例输出)

3)遗憾的是只有32位进程被枚举(下面的示例输出)





这是样本输出。任何帮助将不胜感激!!!





I am facing some issues...

1) I get 0 instead of a handle IntPtr on many processes
2) There where i don't get 0, i get the same handle for more than one process (the sample output below)
3) Only 32bit processes are beign enumerated unfortunately (the sample output below)


This is the sample output. ANY help would be appreciated!!!

556: putty - 32 BIT<br />
556: vmware-unity-helper - 32 BIT<br />
556: Greenshot - 32 BIT<br />
556: RAVCpl64 - 32 BIT<br />
556: notepad - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: vmplayer - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: MOM - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
564: TeamViewer - 32 BIT<br />
564: chrome - 32 BIT<br />
568: calc - 32 BIT<br />
568: explorer - 32 BIT<br />
568: conhost - 32 BIT<br />
568: taskhostex - 32 BIT<br />
568: chrome - 32 BIT<br />
568: xampp-control - 32 BIT<br />
568: chrome - 32 BIT<br />
568: conhost - 32 BIT<br />
568: chrome - 32 BIT<br />
568: notepad++ - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: AdobeIPCBroker - 32 BIT<br />
568: chrome - 32 BIT<br />
568: conhost - 32 BIT<br />
568: IntelliTrace - 32 BIT<br />
568: chrome - 32 BIT<br />
568: bdwtxag - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: MSBuild - 32 BIT<br />
568: CCC - 32 BIT<br />
568: vprintproxy - 32 BIT<br />
568: chrome - 32 BIT<br />
568: putty - 32 BIT<br />
568: chrome - 32 BIT

推荐答案

首先

First of all
if (is32bit = false)



都是错误的(应该是'==';比较,而不是你写的任务)和愚蠢,因为 is32bit 已经是布尔值。应该是:


is both buggy (should be '=='; comparison, not assignment you wrote) and silly, because is32bit is already Boolean. Should be:

if (is32bit)
    // ...



另外,你不应该打开这个过程来获得一个处理。相反,使用 currentProcess.Handle

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.handle%28v=vs.110%29。 aspx [ ^ ]。



可能你需要同时显示句柄 Id ,或者更好的是, Id ,因为句柄几乎没用。只有 Id 适合在不同的进程中使用;这是进程的与进程无关(系统范围)的ID。在不同应用程序域的地址空间中获得的相同进程的句柄将是不同的数值。



-SA


Also, you should not open the process to get a handle. Instead, use currentProcess.Handle:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.handle%28v=vs.110%29.aspx[^].

Probably you need to show both Handle and Id, or, even better, Id only, because Handle is nearly useless to from the user's standpoint. Only the Id is good for using in different processes; this is the process-independent (system-wide) ID of a process. Handles of the same process obtained in the address spaces of different application domains will be different numerical values.

—SA


这篇关于获取C#中所有进程的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆