标签:
代码是网上查找资料,然后自己调试,修改之后可以运行。
系统:win7 32位,VS2008
-----------------------------------------------------------------------代码------------------------------------------------------------------------------------
1 #include <iostream> 2 #include <string> 3 #include <map> 4 #include <windows.h> 5 #include <TlHelp32.h> 6 using namespace std; 7 8 bool traverseProcesses(map<string, int>& _nameID) 9 { 10 PROCESSENTRY32 pe32; 11 pe32.dwSize = sizeof(pe32); 12 13 HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获取进程快照 14 if(hProcessSnap == INVALID_HANDLE_VALUE) { 15 cout << "CreateToolhelp32Snapshot Error!" << endl;; 16 return false; 17 } 18 19 BOOL bResult =Process32First(hProcessSnap, &pe32); 20 21 int num(0); 22 23 while(bResult) 24 { 25 //string name = string(pe32.szExeFile); 26 char temp[300]; 27 WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, -1, temp, sizeof(temp), NULL, NULL); 28 string name = string(temp); 29 int id = pe32.th32ProcessID; 30 31 cout << "[" << ++num << "] : " <<"Process Name:" 32 << name << " " << "ProcessID:" << id<< endl; 33 34 _nameID.insert(pair<string, int>(name, id)); //字典存储 35 bResult = Process32Next(hProcessSnap,&pe32); 36 } 37 38 CloseHandle(hProcessSnap); 39 40 return true; 41 } 42 43 int main() 44 { 45 map<string, int> _nameID; 46 47 if (!traverseProcesses(_nameID)) { 48 cout << "Start Process Error!" << endl; 49 } 50 51 return 0; 52 }
运行结果:
C++获取Windows7 32位系统中所有进程名(类似于任务管理器中的进程)
标签:
原文地址:http://www.cnblogs.com/LCCRNblog/p/4652374.html