本文地址: http://blog.csdn.net/caroline_wendy/article/details/29381987
遍历所有进程, 即任务管理器中所有的进程目录, 包含名称和进程ID.
返回字典: Key: 进程名字, Value: 进程ID.
代码:
/* * main.cpp * * Created on: 2014.06.08 * Author: Spike */ /*vs 2012*/ #include <iostream> #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::endl;; 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::pair<string, int>(name, id)); //字典存储 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; }
说什么呢?题目就叫创业与长跑吧,或者“黄浦江往南,再往南”,布布扣,bubuko.com
原文:http://blog.csdn.net/domypp/article/details/29380991