请问如下关于进程快照的程序本人没调试通过请问那里出错了?

mamingchen 2002-12-27 06:06:15
// Find each process and display it.
ListView1->Items->Clear();
HANDLE snapshot ;
PROCESSENTRY32 processinfo ;//PROCESSENTRY32为列举进程快照函数
processinfo.dwSize = sizeof (processinfo) ;//dwSize为字节分配


snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0) ;//CreateToolhelp32Snapshot创建一个快照


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) ;
}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Find each process and display it.
ListView1->Items->Clear();
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) ;
}

此快照只能反映某个时间片段的进程



...全文
34 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
老兄还称自己入门者

我看到你在文坛中的关于BCB的文章,很不错啊

谦虚的高手,以后多多多写文章
jishiping 2002-12-27
  • 打赏
  • 举报
回复
当然是你调用时的进程列表了。如果需要动态的,就需要用Timer不断检查。
kingfish 2002-12-27
  • 打赏
  • 举报
回复
楼主好像问题是: 此快照只能反映某个时间片段的进程

学习。。。
jishiping 2002-12-27
  • 打赏
  • 举报
回复
"没调试通过请问那里出错了?" 有什么错误?
对于函数,CreateToolhelp32Snapshot,失败时返回-1,而不是NULL。另外,
你上面使用String,多此一举,直接使用字符数组就可以了。
char buffer[512];
HANDLE snapshot;
PROCESSENTRY32 processinfo;

memset(&processinfo, 0, sizeof(processinfo));
processinfo.dwSize = sizeof(processinfo) ;
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ;
if (snapshot == (HANDLE)-1) return;

bool status = Process32First(snapshot, &processinfo);
while(status)
{
TListItem *li = ListView1->Items->Add () ;
sprintf(buffer.c_str (), "%08X", processinfo.th32ProcessID);
li->Caption = buffer;
sprintf(buffer.c_str (), "%08X", processinfo.th32ParentProcessID) ;
li->SubItems->Add(buffer);
li->SubItems->Add(processinfo.szExeFile);
status = Process32Next(snapshot, &processinfo);
}
CloseHandle(hProcessSnap); //最后别忘了关闭句柄

1,222

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧