windows api学习笔记-遍历系统进程,获取进程名称和ID(进程快照)

#include <windows.h>//系统会自动连接到指定的库文件lib
#include <tlhelp32.h>//声明快照函数的头文件
#include <stdio.h>//std  io  标准输入输出接口
#include <iostream>
using namespace std;

int main()
{
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(pe32);
    HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if(hProcessSnap == INVALID_HANDLE_VALUE)
    {
        printf("CreateToolhelp32Snapshot调用失败");
        return -1;
    }
    BOOL bMore = ::Process32First(hProcessSnap,&pe32);
    while(bMore)
    {
        printf("进程名称:%s\n",pe32.szExeFile);
        printf("进程ID:%u\n\n",pe32.th32ProcessID);
        bMore = ::Process32Next(hProcessSnap,&pe32);
    }
    char a;
    cin>>a;
    ::CloseHandle(hProcessSnap);
    return 0;
}

 

posted @ 2022-12-12 15:45  dignitys  阅读(596)  评论(0)    收藏  举报