本文共 1487 字,大约阅读时间需要 4 分钟。
代码是网上查找资料,然后自己调试,修改之后可以运行。
系统:win7 32位,VS2008
-----------------------------------------------------------------------代码------------------------------------------------------------------------------------
1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 8 bool traverseProcesses(map & _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 (name, id)); //字典存储 35 bResult = Process32Next(hProcessSnap,&pe32); 36 } 37 38 CloseHandle(hProcessSnap); 39 40 return true; 41 } 42 43 int main() 44 { 45 map _nameID; 46 47 if (!traverseProcesses(_nameID)) { 48 cout << "Start Process Error!" << endl; 49 } 50 51 return 0; 52 }
运行结果:
转载于:https://www.cnblogs.com/LCCRNblog/p/4652374.html