首页 > 学院 > 开发设计 > 正文

用快照枚举当前系统中所有进程

2019-11-17 05:30:07
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表