如何用快照枚举当前系统中所有进程,近来问这个问题的朋友比较多,所以干脆贴上来算了。呵呵。: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) ;
}
}