
//---------------------------------------------------------------------------
#include <tlhelp32.h> // 添加这行
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Memo1->Clear();
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
Memo1->Lines->Add("错误:无法创建进程快照!");
return;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
int processCount = 0;
if (Process32First(hSnapshot, &pe32))
{
Memo1->Lines->Add("=== 系统进程列表 ===");
Memo1->Lines->Add("序号\tPID\t句柄\t\t进程名称\t\t线程数\t父进程ID");
Memo1->Lines->Add("------------------------------------------------------------");
do
{
processCount++;
// 尝试打开进程获取句柄
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, pe32.th32ProcessID);
String handleStr;
if (hProcess != NULL)
{
handleStr = "0x" + IntToHex((int)hProcess, 8);
CloseHandle(hProcess);
}
else
{
handleStr = "[拒绝访问]";
}
// 格式化输出
String line = Format("%d\t%d\t%s\t%s\t%d\t%d",
ARRAYOFCONST((
processCount,
pe32.th32ProcessID,
handleStr,
pe32.szExeFile,
pe32.cntThreads,
pe32.th32ParentProcessID
)));
Memo1->Lines->Add(line);
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
Memo1->Lines->Add("==========================================");
Memo1->Lines->Add("总计找到 " + IntToStr(processCount) + " 个进程");
}
//---------------------------------------------------------------------------