www.pudn.com > MFCtest.rar > CPDH.cpp
#include "CPDH.h" #include//公有函数 CProcess::CProcess(LPTSTR szMachineName,LPTSTR accont,LPTSTR password) { //检查ip,帐户,密码的格式是否合适 //...... this->szMachineName=new TCHAR[lstrlen(szMachineName)* sizeof (TCHAR)]; this->accont=new TCHAR[lstrlen(accont)* sizeof (TCHAR)]; this->password=new TCHAR[lstrlen(password)* sizeof (TCHAR)]; lstrcpy(this->szMachineName,szMachineName); lstrcpy(this->accont,accont); lstrcpy(this->password,password); ConnIpc(); head=p1=p2=NULL; TCHAR temp[9][32]={_T(""),_T("ID Process"), _T("Creating Process ID"),_T("Elapsed Time"),_T("Thread Count"),_T("% User Time"), _T("Handle Count"),_T("Priority Base"),_T("Working Set")}; for(int i=0;i<9;i++) lstrcpy(counterName[i],temp[i]); } CProcess::CProcess() { head=p1=p2=NULL; szMachineName=NULL; accont=NULL; password=NULL; num=0; TCHAR temp[9][32]={_T(""),_T("ID Process"), _T("Creating Process ID"),_T("Elapsed Time"),_T("Thread Count"),_T("% User Time"), _T("Handle Count"),_T("Priority Base"),_T("Working Set")}; for(int i=0;i<9;i++) lstrcpy(counterName[i],temp[i]); } Process::~Process() { if(szCounterListBuffer != NULL) delete(szCounterListBuffer); if(szInstanceListBuffer != NULL) delete(szInstanceListBuffer); if(szMachineName!=NULL) delete(szMachineName); if(accont!=NULL) delete(accont); if(password!=NULL) delete(password); destroyProcessInfo(); } ProcessInfo *Process::currentProcess() { for(int i=0;i<9;i++) { lstrcpy(current_pi.info[i],p2->info[i]); } current_pi.instanceIndex=p2->instanceIndex; return ¤t_pi; } bool Process::goFirstProcess() { p1=p2=head; return true; } bool Process::goNextProcess() { p1=p2; if((p2->next)!=NULL) { p2=p2->next; return true; } else return false; } bool Process::hasNext() { if(p2->next==NULL) return false; else return true; } int Process::processNum() { return num; } /*********************************************************************/ //私有函数 ProcessInfo *Process::addnew() { if(head==NULL) { head=p1=p2=new ProcessInfo; } else { p2=new ProcessInfo; p1->next=p2; p1=p2; } /* for(int i=0;i<9;i++) { lstrcpy(p2->info[i],pi.info[i]); }*/ //p2->instanceIndex=0; p2->next=NULL; num++; return p2; } bool Process::setInstanceIndex(ProcessInfo *pi) { ProcessInfo *p1,*p2; p1=p2=head; pi->instanceIndex=0; if(head!=NULL) { do { if(p2!=pi) if(lstrcmpi(p2->PRONAME,pi->PRONAME)==0) if(p2->instanceIndex==pi->instanceIndex) pi->instanceIndex=p2->instanceIndex+1; p2=p2->next; p1=p2; }while(p2!=NULL); } return true; } bool Process::getProcessInfo() { szCounterListBuffer = NULL; dwCounterListSize = 0; szInstanceListBuffer = NULL; dwInstanceListSize = 0; hQuery = NULL; hCounter = NULL; TCHAR Buffer[1024]; int i; // Determine the required buffer size for the data. pdhStatus = PdhEnumObjectItems(NULL, szMachineName, _T("Process"), szCounterListBuffer, &dwCounterListSize, szInstanceListBuffer, &dwInstanceListSize, PERF_DETAIL_WIZARD, 0); if ((pdhStatus == ERROR_SUCCESS) || (pdhStatus == PDH_MORE_DATA)) { // Allocate the buffers and try the call again. szCounterListBuffer = new TCHAR[dwCounterListSize * sizeof (TCHAR)]; szInstanceListBuffer = new TCHAR[dwInstanceListSize * sizeof (TCHAR)]; if (szCounterListBuffer != NULL && szInstanceListBuffer != NULL) { pdhStatus = PdhEnumObjectItems(NULL,szMachineName, _T("Process"), szCounterListBuffer, &dwCounterListSize, szInstanceListBuffer, &dwInstanceListSize, PERF_DETAIL_WIZARD, 0); if (pdhStatus == ERROR_SUCCESS) { // Walk the return instance list. if(head!=NULL) destroyProcessInfo(); LPTSTR pInst = szInstanceListBuffer; pdhStatus = PdhOpenQuery (NULL, NULL, &hQuery); num=0; for (; *pInst != 0; pInst += lstrlen(pInst) + 1) { pi=addnew(); lstrcpy(pi->PRONAME,pInst); setInstanceIndex(pi); for(i=1;i<9;i++) { wsprintf(szPathBuffer, _T("%s\\Process(%s#%d)\\%s"), szMachineName, pi->PRONAME,pi->instanceIndex,counterName[i]); //MessageBox(NULL,szPathBuffer,NULL,0); pdhStatus = PdhAddCounter(hQuery, szPathBuffer, 0, &hCounter); pdhStatus = PdhCollectQueryData(hQuery); pdhStatus = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG, &ctrType, &fmtValue); wsprintf(Buffer,_T("%d"),fmtValue.longValue); lstrcpy(pi->info[i],Buffer); //MessageBox(NULL,pi->info[i],pi->PRONAME,0); pdhStatus = PdhRemoveCounter(hCounter); } //MessageBox(NULL,_T("haha1"),_T(""),0); } pdhStatus = PdhCloseQuery (hQuery); return true; } } } return false; } bool Process::ConnIpc() { NETRESOURCE nr; TCHAR RN[50]; wsprintf(RN,_T("\\\\%s\\ipc$"),szMachineName); nr.dwType=RESOURCETYPE_ANY; nr.lpLocalName=NULL; nr.lpRemoteName=RN; nr.lpProvider=NULL; if(WNetAddConnection2(&nr,password,accont,false)==NO_ERROR) return true; else return false; } bool Process::destroyProcessInfo() { while(head!=NULL) { p2=head; head=head->next; if(p2!=NULL) delete(p2); } return true; } ////////////////////////////////////////////////////////////////////// // CPDH Class ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPDH::CPDH() { } CPDH::~CPDH() { }