C++BuilderXE12查询所有进程名程与句柄

//---------------------------------------------------------------------------

#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) + " 个进程");

}

//---------------------------------------------------------------------------

相关推荐
肆忆_10 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星14 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛