日期:2014-05-18 浏览次数:20816 次
using System.Runtime.InteropServices; private const int MAX_PATH = 260; private struct PROCESSENTRY32 { // data members public uint dwSize; public uint cntUsage; public uint th32ProcessID; public uint th32DefaultHeapID; public uint th32ModuleID; public uint cntThreads; public uint th32ParentProcessID; public int pcPriClassBase; public uint dwFlags; [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_PATH)] public byte[] szExeFile; } private const int TH32CS_SNAPPROCESS = 0x00000002; [DllImport("kernel32")] private static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processID); [DllImport("kernel32")] private static extern bool Process32First(IntPtr handle, ref PROCESSENTRY32 procData); [DllImport("kernel32")] private static extern bool Process32Next(IntPtr handle, ref PROCESSENTRY32 procData); [DllImport("kernel32")] private static extern bool CloseHandle(IntPtr handle); private void button1_Click(object sender, EventArgs e) { IntPtr vHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 vProcessEnter = new PROCESSENTRY32(); vProcessEnter.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)); if (Process32First(vHandle, ref vProcessEnter)) { do { int j = 0; for (int i = 0; i < vProcessEnter.szExeFile.Length; i++) if (vProcessEnter.szExeFile[i] == 0) { j = i; break; } Console.WriteLine("进程ID:{0},进程文件名:{1}", vProcessEnter.th32ProcessID, Encoding.Default.GetString(vProcessEnter.szExeFile, 0, j)); } while (Process32Next(vHandle, ref vProcessEnter)); } }