您的位置:首页 > 编程语言 > C#

Wpf 遍历系统进程 C#

2013-01-12 10:26 162 查看
Kernel32 http://[/code] 
点击打开链接

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace THEnumProcesses
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
var handle = Kernel32.CreateToolhelp32Snapshot(0x2, 0);
var list = new List<Kernel32.ProcessEntry32>();
if ((int)handle > 0)
{
var pe32 = new Kernel32.ProcessEntry32();
pe32.dwSize = (uint)Marshal.SizeOf(pe32);
int bMore = Kernel32.Process32First(handle, ref pe32);
while (bMore == 1)
{
var temp = Marshal.AllocHGlobal((int)pe32.dwSize);
Marshal.StructureToPtr(pe32, temp, true);
var pe = (Kernel32.ProcessEntry32)Marshal.PtrToStructure(temp, typeof(Kernel32.ProcessEntry32));
Marshal.FreeHGlobal(temp);
list.Add(pe);
bMore = Kernel32.Process32Next(handle, ref pe32);
}
Kernel32.CloseHandle(handle);

listView1.ItemsSource = list.Select(obj=>new
{
PID = obj.th32ProcessID,
进程名 = obj.szExeFile,
}).ToList();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: