windows mobile进程查看器开发(一)——获得当前运行进程的信息

    技术2026-04-10  1

    windows mobile下进程查看器的开发不算太难,网上也有很多例子可以参考,我也懒得写太多解释,事实上,我也解释不清楚,因为我自己也仍然不是很清楚,所以也就借写博客来理一下自己的思路吧,目前的进程查看器界面如下:

     

    呆是呆了点哈,索性能运行。

     

    首先,进程查看器当然最重要的就是获得当前运行的所有进程的信息,这里我列出的有进程名称,PID,以及该进程下的线程数,本来还想给出进程占用的CPU以及内存的情况,后来发现这是没有现成的API函数可以调用的,也就是要自己来计算的,而且计算结果也不会很精确,所以这部分内容等以后有时间了再钻研钻研~

     

    来看看具体有关获取当前进程信息表的代码:

     

    这里比较重要的有

     

    CreateToolhelp32Snapshot:用来获取当前进程的一个快照的函数

    Process32First: 获得第一个进程的句柄的函数

    Process32Next: 获得下一个进程的句柄的函数

    CloseToolhelp32Snapshot: 关闭快照

    PROCESSENTRY32:用来存放快照进程信息的一个结构体

     

    有关以上函数的具体用法可以查MSDN

     

    class Process { private string processName; private uint processId; private int threadCount; public Process() { } private Process(UInt32 processId, string processName,int threadCount) { this.processId = processId; this.processName = processName; this.threadCount = threadCount; } public override string ToString() { return processName; } public uint ProcessId { get { return processId; } } public string ProcessName { get { return processName; } } public int ThreadCount { get { return threadCount; } } public static Process[] GetProcesses() //得到当前的进程信息表 { ArrayList procList = new ArrayList(); IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0); int n=0; if ((int)handle > 0) { try { PROCESSENTRY32 peCurrent; PROCESSENTRY32 pe32 = new PROCESSENTRY32(); byte[] peBytes = pe32.ToByteArray(); int retval = Process32First(handle, peBytes); while (retval == 1) { peCurrent = new PROCESSENTRY32(peBytes); peCurrent.szExeFile = peCurrent.szExeFile.Replace("/0", ""); Process proc = new Process(((UInt32)peCurrent.PID), peCurrent.szExeFile, (int)peCurrent.ThreadCount); procList.Add(proc); retval = Process32Next(handle, peBytes); } } catch (Exception ex) { throw new Exception("Exception: " + ex.Message); } CloseToolhelp32Snapshot(handle); return (Process[])procList.ToArray(typeof(Process)); } else { throw new Exception("Unable to get process"); } } public class PROCESSENTRY32 { private const int SizeOffset = 0; private const int UsageOffset = 4; private const int ProcessIDOffset = 8; private const int DefaultHeapIDOffset = 12; private const int ModuleIDOffset = 16; private const int ThreadsOffset = 20; private const int ParentProcessIDOffset = 24; private const int PriClassBaseOffset = 28; private const int dwFlagsOffset = 32; private const int ExeFileOffset = 36; private const int MemoryBaseOffset = 556; private const int AccessKeyOffset = 560; private const int Size = 564; private const int MAX_PATH = 260; public uint dwSize; public uint cntUsage; public uint th32ProcessID; public uint th32DefaultHeapID; public uint th32ModuleID; public uint cntThreads; public uint th32ParentProcessID; public long pcPriClassBase; public uint dwFlags; public string szExeFile; public uint th32MemoryBase; public uint th32AccessKey; public PROCESSENTRY32() { } public PROCESSENTRY32(byte[] aData) { dwSize = GetUInt(aData, SizeOffset); cntUsage = GetUInt(aData, UsageOffset); th32ProcessID = GetUInt(aData, ProcessIDOffset); th32DefaultHeapID = GetUInt(aData, DefaultHeapIDOffset); th32ModuleID = GetUInt(aData, ModuleIDOffset); cntThreads = GetUInt(aData, ThreadsOffset); th32ParentProcessID = GetUInt(aData, ParentProcessIDOffset); pcPriClassBase = (long)GetUInt(aData, PriClassBaseOffset); dwFlags = GetUInt(aData, dwFlagsOffset); szExeFile = GetString(aData, ExeFileOffset, MAX_PATH); th32MemoryBase = GetUInt(aData, MemoryBaseOffset); th32AccessKey = GetUInt(aData, AccessKeyOffset); } #region Conversion functions private static uint GetUInt(byte[] aData, int Offset) { return BitConverter.ToUInt32(aData, Offset); } private static void SetUInt(byte[] aData, int Offset, int Value) { byte[] buint = BitConverter.GetBytes(Value); Buffer.BlockCopy(buint, 0, aData, Offset, buint.Length); } private static ushort GetUShort(byte[] aData, int Offset) { return BitConverter.ToUInt16(aData, Offset); } private static void SetUShort(byte[] aData, int Offset, int Value) { byte[] bushort = BitConverter.GetBytes((short)Value); Buffer.BlockCopy(bushort, 0, aData, Offset, bushort.Length); } private static string GetString(byte[] aData, int Offset, int Length) { String sReturn = Encoding.Unicode.GetString(aData, Offset, Length); return sReturn; } private static void SetString(byte[] aData, int Offset, string Value) { byte[] arr = Encoding.ASCII.GetBytes(Value); Buffer.BlockCopy(arr, 0, aData, Offset, arr.Length); } #endregion public byte[] ToByteArray() { byte[] aData; aData = new byte[Size]; SetUInt(aData, SizeOffset, Size); return aData; } public string Name { get { return szExeFile.Substring(0, szExeFile.IndexOf('/0')); } } public ulong PID { get { return th32ProcessID; } } public ulong BaseAddress { get { return th32MemoryBase; } } public ulong ThreadCount { get { return cntThreads; } } } private const int TH32CS_SNAPPROCESS = 0x00000002; private const int TH32CS_SNAPNOHEAPS = 0x40000000; [DllImport("toolhelp.dll")] public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid); [DllImport("toolhelp.dll")] public static extern int CloseToolhelp32Snapshot(IntPtr handle); [DllImport("toolhelp.dll")] public static extern int Process32First(IntPtr handle, byte[] pe); [DllImport("toolhelp.dll")] public static extern int Process32Next(IntPtr handle, byte[] pe); }

     

    最后不要忘了DllImport,因为上面的这些函数并不是C#中本身有的api

     

    最新回复(0)