void FindAllProcessNames(vector<string>& outProcessesContainer )
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (processesSnapshot == INVALID_HANDLE_VALUE)
{
return;
}
//Process First in snapshot and traverse the list.
Process32First(processesSnapshot, &processInfo);
outProcessesContainer.push_back(processInfo.szExeFile);
while ( Process32Next(processesSnapshot, &processInfo) )
{
outProcessesContainer.push_back(processInfo.szExeFile);
}
CloseHandle(processesSnapshot);
}
应用:
#include "windows.h"
#include <string>
#include "tlhelp32.h"
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <vector>
void FindAllProcessNames(std::vector<std::string>& outProcessesContainer )
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (processesSnapshot == INVALID_HANDLE_VALUE)
{
return;
}
//Process First in snapshot and traverse the list.
Process32First(processesSnapshot, &processInfo);
outProcessesContainer.push_back(processInfo.szExeFile);
while ( Process32Next(processesSnapshot, &processInfo) )
{
outProcessesContainer.push_back(processInfo.szExeFile);
}
CloseHandle(processesSnapshot);
}
int _tmain(int argc, _TCHAR* argv[]){
std::vector<std::string> outProcessesContainer;
FindAllProcessNames(outProcessesContainer);
std::vector<std::string>::iterator it;
for(it = outProcessesContainer.begin();it !=outProcessesContainer.end();++it){
std::cout<<*it<<std::endl;
}
return 0;
}
结果:
[b][color=red]
[System Process]
System
smss.exe
csrss.exe
winlogon.exe
services.exe
lsass.exe
ati2evxx.exe
svchost.exe
svchost.exe
svchost.exe
svchost.exe
svchost.exe
ZhuDongFangYu.exe
ati2evxx.exe
spoolsv.exe
ksdsvc.exe
sqlservr.exe
alg.exe
explorer.exe
RTHDCPL.EXE
360tray.exe
ctfmon.exe
360sd.exe
emule.exe
CBTray.exe
msnmsgr.exe
wlcomm.exe
TXPlatform.exe
QQ.exe
TXOPShow.exe
devenv.exe
XDict.exe
QQMusic.exe
360rp.exe
devenv.exe
conime.exe
QQ.exe
mspdbsrv.exe
devenv.exe
QQ.exe
devenv.exe
IEXPLORE.EXE
taskmgr.exe
cmd.exe
cmd.exe
Test.exe
[/color][/b]