Iterate through all processes, that is, all the process directories in Task Manager, including the name and process ID.
Back to Dictionary: Key: Process name, Value: Process ID.
Code:
* * * main.cpp * * Created on:2014.06.08 * author:spike//*vs 2012*/#include <iostre
am> #include <string> #include <map> #include <windows.h> #include <TlHelp32.h>
using namespace Std;
BOOL Traverseprocesses (std::map<std::string, int>& _nameid) {PROCESSENTRY32 pe32;
pe32.dwsize = sizeof (PE32);
HANDLE Hprocesssnap = createtoolhelp32snapshot (th32cs_snapprocess, 0); if (Hprocesssnap = = Invalid_handle_value) {std::cout << "createtoolhelp32snapshot error!" << std::en
DL;;
return false;
BOOL bresult =process32first (Hprocesssnap, &pe32);
int num (0);
while (bresult) {std::string name = Pe32.szexefile;
int id = PE32.TH32PROCESSID; Std::cout << "[<< ++num <<"]: "<<" Process Name: "<< NAme << "<<" ProcessID: "<< id<< Std::endl; _nameid.insert (std::p air<string, int> (name, id));
Dictionary Storage bresult = Process32Next (HPROCESSSNAP,&PE32);
} closehandle (HPROCESSSNAP);
return true;
int main () {std::map<std::string, int> _nameid;
if (!traverseprocesses (_nameid)) {cout << "Start Process error!" << Endl;
return 0; }
Output:
Author: csdn Blog spike_king
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/