/* includes */ #include #include #include #include #include #include using namespace std ; /* defines */ #define ALIGN 26 //----< outputs string in Heading format >---- void Title ( const std::string & str, char ch ) { for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) std::cout << ch ; std::cout << "\n" << str << "\n" ; for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) std::cout << ch ; std::cout << "\n\n" ; } //----< retrieve and display additional info on specified thread >---- // params : const HANDLE & hThr - Handle to thread // return : void // exceptions : none // desciptions : // notes : //--------------------------------------------------------------------- void ThreadQuery ( const HANDLE & hThr ) { /* thread priority */ ///////////////////// int pri ; if ((pri = GetThreadPriority(hThr)) != THREAD_PRIORITY_ERROR_RETURN) { cout << setw(ALIGN) << " thread priority" << " : " ; if ( pri == THREAD_PRIORITY_ABOVE_NORMAL ) cout << "THREAD_PRIORITY_ABOVE_NORMAL\n" ; else if ( pri == THREAD_PRIORITY_BELOW_NORMAL ) cout << "THREAD_PRIORITY_BELOW_NORMAL\n" ; else if ( pri == THREAD_PRIORITY_HIGHEST ) cout << "THREAD_PRIORITY_HIGHEST\n" ; else if ( pri == THREAD_PRIORITY_IDLE ) cout << "THREAD_PRIORITY_IDLE\n" ; else if ( pri == THREAD_PRIORITY_LOWEST ) cout << "THREAD_PRIORITY_LOWEST\n" ; else if ( pri == THREAD_PRIORITY_NORMAL ) cout << "THREAD_PRIORITY_NORMAL\n" ; else if ( pri == THREAD_PRIORITY_TIME_CRITICAL ) cout << "THREAD_PRIORITY_TIME_CRITICAL\n" ; } /* thread priority boost */ /////////////////////////// BOOL boostdisable ; if ( GetThreadPriorityBoost ( hThr, & boostdisable ) != 0 ) { cout << setw(ALIGN) << " priority boost disabled" << " : " ; if (boostdisable == TRUE) cout << "yes\n" ; else cout << "no\n" ; } /* thread times */ ////////////////// FILETIME ftCreation, ftExit, ftKernel, ftUser ; SYSTEMTIME stCreation, stExit, stKernel, stUser ; if (GetThreadTimes ( hThr, & ftCreation, & ftExit, & ftKernel, & ftUser ) != 0) { if (FileTimeToSystemTime (& ftCreation, & stCreation) == TRUE) cout << setw(ALIGN) << " thread creation time" << " : " << dec << stCreation.wYear << "/" << dec << stCreation.wDay << "/" << dec << stCreation.wMonth << " " << dec << stCreation.wHour << ":" << dec << stCreation.wMinute << ":" << dec << stCreation.wSecond << ":" << dec << stCreation.wMilliseconds << "\n" ; if (FileTimeToSystemTime (& ftKernel, & stKernel) == TRUE) cout << setw(ALIGN) << " thread kernel time" << " : " << dec << stKernel.wHour << ":" << dec << stKernel.wMinute << ":" << dec << stKernel.wSecond << ":" << dec << stKernel.wMilliseconds << "\n" ; if (FileTimeToSystemTime (& ftUser, & stUser) == TRUE) cout << setw(ALIGN) << " thread user time" << " : " << dec << stUser.wHour << ":" << dec << stUser.wMinute << ":" << dec << stUser.wSecond << ":" << dec << stUser.wMilliseconds << "\n" ; } //////////////// // WIN XP SP1 // //////////////// // GetThreadIOPendingFlag } //----< retrieve info on all threads associated with a process >---- // params : const PROCESSENTRY32 & pe32 - tlhelp struct for process // return : void // exceptions : none // desciptions : // notes : //--------------------------------------------------------------------- void ProcessThreadsEnum ( const PROCESSENTRY32 & pe32 ) { /* get snapshot of all threads for given process */ /////////////////////////////////////////////////// HANDLE hThreadSnap ; if ((hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pe32.th32ProcessID)) == INVALID_HANDLE_VALUE) { CloseHandle ( hThreadSnap ) ; cout << setw(ALIGN) << " Thread Enumeration failed !\n\n" ; return ; } /* get information of first thread associated with the process */ ///////////////////////////////////////////////////////////////// THREADENTRY32 te32 ; te32.dwSize = sizeof ( THREADENTRY32 ) ; /* Set size before using */ if( ! Thread32First( hThreadSnap, & te32 ) ) { CloseHandle ( hThreadSnap ) ; /* dispose of handle */ cout << setw(ALIGN) << " Thread Enumeration failed !\n\n" ; return ; } /* now enumerate through rest of the associated threads */ ////////////////////////////////////////////////////////// do { /* NOTE : Enumeration shows all threads on system. */ /* Display thread-info only for threads associated */ /* with current process. */ ////////////////////////////////////////////////////// if ( te32.th32OwnerProcessID == pe32.th32ProcessID ) { cout << "\n" << setw(ALIGN) << " THREAD ID" << " : 0x" << hex << te32.th32ThreadID << "\n" /* thread id */ << setw(ALIGN) << " owner process id" << " : 0x" << hex << te32.th32OwnerProcessID << "\n" /* owner process id */ << setw(ALIGN) << " thread base priority" << " : 0x " << hex << te32.tpBasePri << "\n" ; /* thread base priority */ /* For additional info on this thread, we need to first */ /* obtain its handle */ ////////////////////////////////////////////////////////////// HANDLE hThr ; if ((hThr = OpenThread (THREAD_QUERY_INFORMATION, FALSE, te32.th32ThreadID)) != NULL) { ThreadQuery ( hThr ) ; /* get more process info */ CloseHandle ( hThr ) ; /* close handle */ } } } while ( Thread32Next ( hThreadSnap, & te32 ) ) ; CloseHandle( hThreadSnap ) ; /* dispose of handle */ return ; } //----< retrieve info on all modules associated with a process >---- // params : PROCESSENTRY32 & pe32 - tool help struct for process // return : void // exceptions : none // desciptions : // notes : //--------------------------------------------------------------------- void ProcessModulesEnum ( const PROCESSENTRY32 & pe32 ) { /* get snapshot of all modules for given process */ /////////////////////////////////////////////////// HANDLE hModuleSnap ; if ((hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pe32.th32ProcessID))==INVALID_HANDLE_VALUE) { CloseHandle ( hModuleSnap ) ; cout << setw(ALIGN) << " Module Enumeration failed !\n\n" ; return ; } /* get information of first module associated with the process */ ///////////////////////////////////////////////////////////////// MODULEENTRY32 me32 ; me32.dwSize = sizeof ( MODULEENTRY32 ) ; /* Set size before using */ if ( ! Module32First( hModuleSnap, & me32 ) ) { CloseHandle ( hModuleSnap ) ; /* dispose of handle */ cout << setw(ALIGN) << " Module Enumeration failed !\n\n" ; return ; } /* now enumerate through rest of the associated modules */ ////////////////////////////////////////////////////////// do { cout << "\n" << setw(ALIGN) << " MODULE NAME" << " : " << me32.szModule << "\n" /* module name */ << setw(ALIGN) << " module base address" << " : 0x" << hex << (unsigned long) me32.modBaseAddr << "\n" /* module base address */ << setw(ALIGN) << " module size" << " : 0x" << hex << me32.modBaseSize << "bytes\n" /* module size in bytes */ << setw(ALIGN) << " process id" << " : 0x" << hex << me32.th32ProcessID << "\n" /* process id */ << setw(ALIGN) << " global ref count" << " : 0x" << hex << me32.GlblcntUsage << "\n" /* global useage count */ << setw(ALIGN) << " local ref count" << " : 0x" << hex << me32.ProccntUsage << "\n" /* process usage count */ << setw(ALIGN) << " module path" << " : " << me32.szExePath << "\n" ; /* full module path */ } while( Module32Next ( hModuleSnap, & me32 ) ) ; CloseHandle( hModuleSnap ) ; /* dispose of handle */ return ; } //----< retrieve and display additional info on specified process >---- // params : HANDLE & hProc - Handle to process // return : void // exceptions : none // desciptions : // notes : //--------------------------------------------------------------------- void ProcessQuery ( const HANDLE & hProc ) { /* process priority class */ //////////////////////////// DWORD pclass ; if ((pclass = GetPriorityClass ( hProc )) != 0) { cout << setw(ALIGN) << "priority class" << " : " ; if ( pclass == ABOVE_NORMAL_PRIORITY_CLASS ) cout << "ABOVE_NORMAL_PRIORITY_CLASS\n" ; else if ( pclass == BELOW_NORMAL_PRIORITY_CLASS ) cout << "BELOW_NORMAL_PRIORITY_CLASS\n" ; else if ( pclass == HIGH_PRIORITY_CLASS ) cout << "HIGH_PRIORITY_CLASS\n" ; else if ( pclass == IDLE_PRIORITY_CLASS ) cout << "IDLE_PRIORITY_CLASS\n" ; else if ( pclass == NORMAL_PRIORITY_CLASS ) cout << "NORMAL_PRIORITY_CLASS\n" ; else if ( pclass == REALTIME_PRIORITY_CLASS ) cout << "REALTIME_PRIORITY_CLASS\n" ; } /* process priority boost */ //////////////////////////// BOOL boostdisable ; if ( GetProcessPriorityBoost ( hProc, & boostdisable ) != 0 ) { cout << setw(ALIGN) << "priority boost disabled" << " : " ; if (boostdisable == TRUE) cout << "yes\n" ; else cout << "no\n" ; } /* process affinity mask */ /////////////////////////// DWORD pMask, sMask ; if ( GetProcessAffinityMask ( hProc, &pMask, &sMask ) == TRUE ) cout << setw(ALIGN) << "process affinity mask" << " : 0x" << hex << pMask << "\n" ; /* process times */ /////////////////// FILETIME ftCreation, ftExit, ftKernel, ftUser ; SYSTEMTIME stCreation, stExit, stKernel, stUser ; if (GetProcessTimes ( hProc, & ftCreation, & ftExit, & ftKernel, & ftUser ) != 0) { if (FileTimeToSystemTime (& ftCreation, & stCreation) == TRUE) cout << setw(ALIGN) << "process creation time" << " : " << dec << stCreation.wYear << "/" << dec << stCreation.wDay << "/" << dec << stCreation.wMonth << " " << dec << stCreation.wHour << ":" << dec << stCreation.wMinute << ":" << dec << stCreation.wSecond << ":" << dec << stCreation.wMilliseconds << "\n" ; if (FileTimeToSystemTime (& ftKernel, & stKernel) == TRUE) cout << setw(ALIGN) << "process kernel time" << " : " << dec << stKernel.wHour << ":" << dec << stKernel.wMinute << ":" << dec << stKernel.wSecond << ":" << dec << stKernel.wMilliseconds << "\n" ; if (FileTimeToSystemTime (& ftUser, & stUser) == TRUE) cout << setw(ALIGN) << "process user time" << " : " << dec << stUser.wHour << ":" << dec << stUser.wMinute << ":" << dec << stUser.wSecond << ":" << dec << stUser.wMilliseconds << "\n" ; } //////////////// // Win Xp SP1 // //////////////// ///* retrieve the process handle count */ ///////////////////////////////////////// //DWORD hndCnt ; //if ((GetProcessHandleCount (hProc, & hndCnt)) != 0) // cout << setw(28) << "process handle count : 0x" // << hex << hndCnt << "\n" ; /* get process's I/O operation info */ ////////////////////////////////////// IO_COUNTERS ioCntrs ; if (( GetProcessIoCounters (hProc, & ioCntrs)) != 0) cout << setw(ALIGN) << "read operation count" << " : 0x" << hex << ioCntrs.ReadOperationCount << "\n" << setw(ALIGN) << "write operation count" << " : 0x" << hex << ioCntrs.WriteOperationCount << "\n" << setw(ALIGN) << "other operation count" << " : 0x" << hex << ioCntrs.OtherOperationCount << "\n" << setw(ALIGN) << "read transfer count" << " : 0x" << hex << ioCntrs.ReadTransferCount << "\n" << setw(ALIGN) << "write transfer count" << " : 0x" << hex << ioCntrs.WriteTransferCount << "\n" << setw(ALIGN) << "other transfer count" << " : 0x" << hex << ioCntrs.OtherTransferCount << "\n" ; ///////////////////// // Win server 2003 // ///////////////////// // GetLogicalProcessorInformation () /* gui resources */ /////////////////// cout << setw(ALIGN) << "handles to gdi objects" << " : 0x" << hex << GetGuiResources ( hProc, GR_GDIOBJECTS ) << "\n" ; cout << setw(ALIGN) << "handles to user objects" << " : 0x" << hex << GetGuiResources ( hProc, GR_USEROBJECTS ) << "\n" ; /* working set size */ ////////////////////// SIZE_T minwsz, maxwsz ; if ( GetProcessWorkingSetSize ( hProc, & minwsz, & maxwsz ) != 0 ) cout << setw(ALIGN) << "min working set size" << " : " << (unsigned long) minwsz << " bytes\n" << setw(ALIGN) << "max working set size" << " : " << (unsigned long) maxwsz << " bytes\n" ; } //----< retrieves and displays info on all running processes >---- // params : none // return : void // exceptions : none // desciption : // notes : //---------------------------------------------------------------- void ProcessEnum () { /* get snapshot of all processes */ /////////////////////////////////// HANDLE hProcessSnap ; if ((hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE) { CloseHandle ( hProcessSnap ) ; cout << "Process Enumeration failed !\n\n" ; return ; } /* retrieve info on first process */ //////////////////////////////////// PROCESSENTRY32 pe32 ; pe32.dwSize = sizeof ( PROCESSENTRY32 ) ; /* Set size before using */ if ( Process32First ( hProcessSnap, & pe32 ) == FALSE ) { CloseHandle ( hProcessSnap ) ; cout << "Process Enumeration failed !\n\n" ; return ; } /* now enumerate through rest of the associated modules */ ////////////////////////////////////////////////////////// do { std::string title = "PROCESS NAME : " ; title.append ( pe32.szExeFile ) ; Title ( title, '#' ) ; cout << setw(ALIGN) << left << "process id" << " : 0x" << hex << pe32.th32ProcessID << "\n" /* pid */ << setw(ALIGN) << left << "thread count" << " : 0x" << hex << pe32.cntThreads << "\n" /* nos of threads */ << setw(ALIGN) << left << "parent process' id" << " : 0x" << hex << pe32.th32ParentProcessID << "\n" /* parent's pid */ << setw(ALIGN) << left << "base priority level" << " : 0x" << hex << pe32.pcPriClassBase << "\n" ; /* process base priority level */ /* For additional info on this process, we need to first */ /* obtain its handle */ ////////////////////////////////////////////////////////////// HANDLE hProc ; if ((hProc = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID)) != NULL) ProcessQuery ( hProc ) ; /* get more process info */ CloseHandle ( hProc ) ; /* close handle */ /* For this process, we will obtain its */ /* module and thread information */ ////////////////////////////////////////////// ProcessThreadsEnum ( pe32 ) ; ProcessModulesEnum ( pe32 ) ; cout << "\n\n" ; } while ( Process32Next ( hProcessSnap, & pe32 ) == TRUE ) ; CloseHandle ( hProcessSnap ) ; /* close snapshot handle */ return ; } //----< program entry point >---- void main () { ProcessEnum () ; }