如何用快照枚舉當前中所有進程,近來問這個問題的朋友比較多,所以干脆貼上來算了。呵呵。: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) ;
}
}