获取windows系统所有运行进程

#include  
包含此头文件


bool traverseProcesses(map& _nameID)   
{  
    PROCESSENTRY32 pe32;  
    pe32.dwSize = sizeof(pe32); //MSDN中描述初始化dwsize,否则获取Process32First fail

    HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获取进程快照  
    if(hProcessSnap == INVALID_HANDLE_VALUE) {  
        cout << "CreateToolhelp32Snapshot Error!" << endl;;  
        return false;  
    }  

    BOOL bResult =Process32First(hProcessSnap, &pe32);  //获取第一个进程

    int num(0);  

    while(bResult)   
    {  
        //string name = string(pe32.szExeFile);
        char temp[300];
        //pe32.szExeFile为获取当前进程名,wideCharToMultiByte为字符类型转换
 
  
 
  

 
  
        WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, -1, temp, sizeof(temp), NULL, NULL); 
        string name = string(temp); 
        int id = pe32.th32ProcessID; //获取进程ID索引
       cout << "[" << ++num << "] : " <<"Process Name:" << name << " " << "ProcessID:" << 
        id<< endl; _nameID.insert(pair(name, id)); //字典存储 bResult = Process32Next(hProcessSnap,&pe32); }
       CloseHandle(hProcessSnap); return true;
   }


 //简单版本 
  

		#include  
		PROCESSENTRY32 pe32;
		pe32.dwSize = sizeof(pe32);

		HANDLE hprocessHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
		bool bResult = Process32First(hprocessHandle, &pe32);
		while(bResult)
		{
			pe32.szExeFile;
			bResult = Process32Next(hprocessHandle,&pe32);  
		}
		CloseHandle(hprocessHandle);



 
  
 
 

你可能感兴趣的:(获取进程;进程名)