用快照枚举当前系统中所有进程

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

如何用快照枚举当前系统中所有进程,近来问这个问题的朋友比较多,所以干脆贴上来算了。呵呵。:D

在窗体上添加一个ListView,设置其ViewStyle为vsReport,在ListView上添加三个Column,再添加一个Button。

#include <tlhelp32.h>

#include "stdio.h"

void __fastcall TMainForm::Button1Click(TObject *Sender)

{

// Find each process and display it.

HANDLE snapshot ;

PROCESSENTRY32 processinfo ;

processinfo.dwSize = sizeof (processinfo) ;

snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0) ;

if (snapshot == NULL)

return ;

bool status = Process32First (snapshot, &processinfo) ;

while (status)

{

TListItem *li = ListView1->Items->Add () ;

String buffer ;

int length ;

buffer.SetLength (512) ;

length = sprintf (buffer.c_str (), "%08X", processinfo.th32ProcessID) ;

buffer.SetLength (length) ;

li->Caption = buffer;

buffer.SetLength (512) ;

length = sprintf (buffer.c_str (), "%08X", processinfo.th32ParentProcessID) ;

buffer.SetLength (length) ;

li->SubItems->Add (buffer) ;

li->SubItems->Add (processinfo.szExeFile) ;

status = Process32Next (snapshot, &processinfo) ;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航