close
  /*  
  * main.cpp  
  *  
  * Created on: 2014.06.08  
  *   Author: Spike  
  */  
 /*vs 2012*/  
 #include <iostream>  
 #include <string>  
 #include <map>  
 #include <windows.h>  
 #include <TlHelp32.h>  
 #include <vector>   
 #include <clocale>  
 using namespace std;  
 // wstring -> string  
 std::string& to_string(std::string& dest, std::wstring const & src)  
 {  
      std::setlocale(LC_CTYPE, "");  
      size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);  
      std::vector<char> tmp(mbs_len + 1);  
      wcstombs(&tmp[0], src.c_str(), tmp.size());  
      dest.assign(tmp.begin(), tmp.end() - 1);  
      return dest;  
 }  
 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::wstring name = pe32.szExeFile;  
           int id = pe32.th32ProcessID;  
           string strName;  
           to_string(strName,name);  
           cout << "[" << ++num << "] : " <<"Process Name:"   
                << strName << " " << "ProcessID:" << id<< std::endl;  
           _nameID.insert(std::pair<string, int>(strName, 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;  
 }  

 

 

arrow
arrow

    Eric 發表在 痞客邦 留言(0) 人氣()