将当前运行的进程列举出来(C++ Code):
#include<windows.h>
#include<tlhelp32.h>
//#include <iomanip>
#include<iostream>
using namespace std;
int main()
{
int count=0;
PROCESSENTRY32 pe32;
//使用这个数据之前设置大小
pe32.dwSize=sizeof(pe32);
HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap==INVALID_HANDLE_VALUE)
{
cout<<"CreateToolhelp32Snapshot调用失败!"<<endl;
return -1;
}
//遍历进程快照,轮流显示每个进程信息
BOOL bMore=::Process32First(hProcessSnap,&pe32);
cout<<"进程名称/t/t/t/t/t进程ID号"<<endl;
while(bMore)
{
//stricmp():比较字符串时忽略大小写;strcmp:比较字符串时,不会忽略大小写
if(stricmp(pe32.szExeFile, "QQ.exe") == 0) //判断当前进程名是不是QQ.exe
cout<<"Find the process-QQ.exe, PID="<<pe32.th32ProcessID<<endl;
//cout<<pe32.szExeFile<<"/t/t/t/t/t"<<pe32.th32ProcessID<<endl; //输出进程名和进程ID
count++;
bMore=::Process32Next(hProcessSnap,&pe32);
}
cout<<"当前运行进程总数为:"<<count<<endl;
::CloseHandle(hProcessSnap);
return 0;
}