//////////////////////////////////
// file: processTree.cpp
//////////////////////////////////
#include <vector> #include <algorithm> #include <iostream> #include <windows.h> #include <tlhelp32.h>
#include "processTree.h"
using namespace std;
bool ProcessTree::getAllProcess(ProcessVector& pv) { HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; DWORD dwPriorityClass;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( hProcessSnap == INVALID_HANDLE_VALUE ) { cout << "error\n"; return false; }
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) ) { cout << "error\n"; CloseHandle( hProcessSnap ); // clean the snapshot object
return false; }
pv.clear(); // Now walk the snapshot of processes, and
// display information about each process in turn
do { ProcessInfo pi(pe32.th32ParentProcessID, pe32.th32ProcessID, std::string(pe32.szExeFile)); pv.push_back(pi); } while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap ); return true; }
Node* ProcessTree::findNode(Node* pNode, DWORD& id) { if (pNode == NULL) { return NULL; } if (pNode->_id == id) { return pNode; } for(NodeVector::iterator it = pNode->_nodeVector.begin(); it != pNode->_nodeVector.end(); ++it) { Node* tmpNode = findNode(&(*it), id); if (tmpNode != NULL) { return tmpNode; } } return NULL; }
bool ProcessTree::insertNode(ProcessInfo& pi) { if (rootVector.empty()) { rootVector.push_back(Node(pi._ID, pi._name)); return true; }
bool findPID = false; for (NodeVector::iterator it = rootVector.begin(); it != rootVector.end(); ++it) { Node* pNode = this->findNode(&(*it), pi._pID); if (pNode != NULL) { findPID = true; pNode->_nodeVector.push_back(Node(pi._ID, pi._name)); break; } } if (!findPID) { rootVector.push_back(Node(pi._ID, pi._name)); //printf("\n%d\t%d\t%s\n", pi._pID, pi._ID, pi._name.c_str());
} return true; } ProcessTree::ProcessTree(ProcessVector& pv) { for(ProcessVector::iterator it = pv.begin();it != pv.end(); ++it) { insertNode(*it); } }
std::string ProcessTree::printInfo(Node* pNode, int deepth) { std::string info; if (pNode == NULL) { return info; } std::string format_str = "\n"; for(int i=0; i<deepth-1; ++i) { format_str += "\t"; ; } if (deepth>0) { format_str += " |----"; } info += format_str; char char_id[10]; sprintf(char_id, "%d", pNode->_id); info += string(char_id); info += ":"; info += pNode->_name;
if (pNode->_nodeVector.empty()) { return info; } else { std::string child_str; for(NodeVector::iterator it = pNode->_nodeVector.begin(); it != pNode->_nodeVector.end(); ++it) { child_str += printInfo(&(*it), deepth+1); } return info + child_str; } } std::string ProcessTree::printInfo() { std::string info; for(NodeVector::iterator it = rootVector.begin(); it != rootVector.end(); ++it) { info += printInfo(&(*it), 1); } return info; } void ProcessTree::printInfo(std::ostream& os) { os << printInfo(); }
std::ostream& operator <<(ostream& os, ProcessTree& pt) { pt.printInfo(os); return os; }
|