home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / pserv.cpl / pserv-2.4.exe / source / cprocesslist.cpp < prev    next >
C/C++ Source or Header  |  2005-01-05  |  8KB  |  225 lines

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "CProcessList.h"
  4. #include "CConfiguration.h"
  5. #include "Aclapi.h"
  6. #include "psapi.H"
  7. #include "2kwrapper.h"
  8.  
  9. static CString strThisProcessUser;
  10.  
  11.  
  12.  
  13. CString CProcessInfo::GetDisplayString(DWORD dwIndex)
  14. {
  15.     switch(dwIndex)
  16.     {
  17.     case 0: return m_strFileName;
  18.     case 1: return m_strProcessID;
  19.     case 2: return m_strUsername;
  20.     case 3: return m_strPageFaultCount;
  21.     case 4: return m_strPeakWorkingSetSize;
  22.     case 5: return m_strWorkingSetSize;
  23.     case 6: return m_strQuotaPeakPagedPoolUsage;
  24.     case 7: return m_strQuotaPagedPoolUsage;
  25.     case 8: return m_strQuotaPeakNonPagedPoolUsage;
  26.     case 9: return m_strQuotaNonPagedPoolUsage;
  27.     case 10: return m_strPagefileUsage;
  28.     case 11: return m_strPeakPagefileUsage;
  29.     }
  30.     return CString();
  31. }
  32.  
  33. COLORREF CProcessInfo::GetTextColor()
  34. {
  35.     return m_dwColor;
  36. }
  37.  
  38. CString CProcessInfo::GetInfoTip()
  39. {
  40.     return m_strFileName;
  41. }
  42.  
  43. CProcessInfo::CProcessInfo( HANDLE hProcess, DWORD dwProcessID, HMODULE* lphModules, DWORD dwModuleCount )
  44.     :   m_dwProcessID( dwProcessID )
  45. {
  46.     m_strProcessID.Format(_T("%ld"), dwProcessID );
  47.     TCHAR szFileName[1024];
  48.     
  49.     for( DWORD dwIndex = 0; dwIndex < dwModuleCount; dwIndex++ )
  50.     {
  51.         if( GetModuleFileNameEx( hProcess, lphModules[dwIndex], szFileName, sizeof(szFileName) ) )
  52.         {
  53.             if( (lpfnGetLongPathName != NULL) &&
  54.                 (tstrchr(szFileName,'~') != NULL) )
  55.             {
  56.                 TCHAR szLongPath[1024];
  57.                 if( lpfnGetLongPathName(szFileName,szLongPath,sizeof(szLongPath)) )
  58.                 {
  59.                     lstrcpy(szFileName,szLongPath);
  60.                 }
  61.             }
  62.             if( !dwIndex )
  63.             {
  64.                 m_strFileName = szFileName;
  65.             }
  66.             else break;
  67.                 //m_Modules.AddTail( new ModuleInformation( szFileName ) );
  68.         }
  69.     }
  70.  
  71.     m_strUsername = GetUserNameFromProcess(hProcess);
  72.  
  73.     m_dwColor = (m_strUsername.CompareNoCase( strThisProcessUser ) == 0) ? RGB(0,0,0) : RGB(128, 128, 128);
  74.  
  75.     ZeroMemory( &m_mc, sizeof(m_mc));
  76.     if( GetProcessMemoryInfo(hProcess, (PPROCESS_MEMORY_COUNTERS) szFileName, sizeof(szFileName) ) )
  77.     {
  78.         PPROCESS_MEMORY_COUNTERS pmc = PPROCESS_MEMORY_COUNTERS(szFileName);
  79.         m_mc = *pmc;
  80.  
  81.         m_strPageFaultCount.Format(_T("%ld"), pmc->PageFaultCount );
  82.         m_strPeakWorkingSetSize.Format(_T("%ld"), pmc->PeakWorkingSetSize );
  83.         m_strWorkingSetSize.Format(_T("%ld"), pmc->WorkingSetSize );
  84.         m_strQuotaPeakPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPeakPagedPoolUsage );;
  85.         m_strQuotaPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPagedPoolUsage );
  86.         m_strQuotaPeakNonPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPeakNonPagedPoolUsage );
  87.         m_strQuotaNonPagedPoolUsage.Format(_T("%ld"), pmc->QuotaNonPagedPoolUsage );
  88.         m_strPagefileUsage.Format(_T("%ld"), pmc->PagefileUsage );
  89.         m_strPeakPagefileUsage.Format(_T("%ld"), pmc->PeakPagefileUsage );
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. #define PROCESS_SORT_STRING_METHOD(NAME)                                            \
  96. static int SortBy##NAME(const CProcessInfo** ps1, const CProcessInfo** ps2)         \
  97. {                                                                                   \
  98.     return (*ps1)->m_str##NAME.CompareNoCase((*ps2)->m_str##NAME);                  \
  99. }
  100.  
  101. PROCESS_SORT_STRING_METHOD(Username)
  102. PROCESS_SORT_STRING_METHOD(FileName)
  103.  
  104. #define PROCESS_SORT_MEMORY_COUNTERS(NAME)                                          \
  105. static int SortBy##NAME(const CProcessInfo** ps1, const CProcessInfo** ps2)         \
  106. {                                                                                   \
  107.     return LONG((*ps1)->m_mc.NAME)-LONG((*ps2)->m_mc.NAME);                         \
  108. }
  109.  
  110. static int SortByProcessID(const CProcessInfo** ps1, const CProcessInfo** ps2)
  111. {
  112.     return LONG((*ps1)->m_dwProcessID)-LONG((*ps2)->m_dwProcessID);
  113. }
  114.  
  115. PROCESS_SORT_MEMORY_COUNTERS(PageFaultCount)
  116. PROCESS_SORT_MEMORY_COUNTERS(PeakWorkingSetSize)
  117. PROCESS_SORT_MEMORY_COUNTERS(WorkingSetSize)
  118. PROCESS_SORT_MEMORY_COUNTERS(QuotaPeakPagedPoolUsage)
  119. PROCESS_SORT_MEMORY_COUNTERS(QuotaPagedPoolUsage)
  120. PROCESS_SORT_MEMORY_COUNTERS(QuotaPeakNonPagedPoolUsage)
  121. PROCESS_SORT_MEMORY_COUNTERS(QuotaNonPagedPoolUsage)
  122. PROCESS_SORT_MEMORY_COUNTERS(PagefileUsage)
  123. PROCESS_SORT_MEMORY_COUNTERS(PeakPagefileUsage)
  124.  
  125. static GENERICCOMPAREFN SortMethods[] = {
  126.     GENERICCOMPAREFN(&SortByFileName),
  127.     GENERICCOMPAREFN(&SortByProcessID),
  128.     GENERICCOMPAREFN(&SortByUsername),
  129.     GENERICCOMPAREFN(&SortByPageFaultCount),
  130.     GENERICCOMPAREFN(&SortByPeakWorkingSetSize),
  131.     GENERICCOMPAREFN(&SortByWorkingSetSize),
  132.     GENERICCOMPAREFN(&SortByQuotaPeakPagedPoolUsage),
  133.     GENERICCOMPAREFN(&SortByQuotaPagedPoolUsage),
  134.     GENERICCOMPAREFN(&SortByQuotaPeakNonPagedPoolUsage),
  135.     GENERICCOMPAREFN(&SortByQuotaNonPagedPoolUsage),
  136.     GENERICCOMPAREFN(&SortByPagefileUsage),
  137.     GENERICCOMPAREFN(&SortByPeakPagefileUsage),
  138. };
  139.  
  140. CProcessList::CProcessList()
  141.     :   CListViewEntries(SortMethods, _T("ProcessList"))
  142. {
  143.     CreateColumns( _T("Process"), _T("PID"), _T("Username"), 
  144. _T("PageFaultCount"),
  145. _T("PeakWorkingSetSize"),
  146. _T("WorkingSetSize"),
  147. _T("QuotaPeakPagedPoolUsage"),
  148. _T("QuotaPagedPoolUsage"),
  149. _T("QuotaPeakNonPagedPoolUsage"),
  150. _T("QuotaNonPagedPoolUsage"),
  151. _T("PagefileUsage"),
  152. _T("PeakPagefileUsage"), NULL );
  153.     
  154.     m_strTitle.Format( TEXT("pserv ") CURRENT_VERSION TEXT(": processes on local machine") );
  155.  
  156.     strThisProcessUser = GetUserNameFromProcess(GetCurrentProcess());
  157. }
  158.  
  159. BOOL CProcessList::Refresh()
  160. {
  161.     DWORD dwArrayOfProcessIds[1024], dwNeeded;
  162.  
  163.     DeleteObjects(m_Entries);
  164.  
  165.     if( !EnumProcesses( dwArrayOfProcessIds, sizeof(dwArrayOfProcessIds), &dwNeeded ) )
  166.         return FALSE;
  167.  
  168.     // Calculate how many process IDs were returned
  169.     DWORD dwCount = dwNeeded / sizeof(DWORD);
  170.  
  171.     for( DWORD dwIndex = 0; dwIndex < dwCount; dwIndex++ )
  172.     {
  173.         DWORD dwProcessID = dwArrayOfProcessIds[dwIndex];
  174.         if( dwProcessID > 2 )
  175.         {
  176.             HANDLE hProcess = OpenProcess( PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwProcessID );
  177.             if( hProcess )
  178.             {
  179.                 HMODULE hArray[1024];
  180.                 DWORD dwNeeded, dwCount;
  181.  
  182.                 if( EnumProcessModules(hProcess,hArray,sizeof(hArray),&dwNeeded ) )
  183.                 {
  184.                     dwCount = dwNeeded / sizeof(HMODULE);
  185.                     
  186.                     m_Entries.Add( new CProcessInfo( hProcess, dwProcessID, hArray, dwCount ) );
  187.                 }
  188.                 CloseHandle( hProcess );
  189.             }
  190.         }
  191.     }
  192.     return TRUE;
  193. }
  194.  
  195. void CProcessList::ConnectTo( LPCTSTR lpszMachine )
  196. {
  197.     // TODO: display error msg here ....
  198.     //m_strServiceMachine = lpszMachine;
  199. }
  200.  
  201. void CProcessList::ExportXmlToFile( CFile* pFile )
  202. {
  203.     PrintToFile(pFile, _T("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\r\n")
  204.               _T("<processlist>\r\n") );
  205.                                
  206.     for(int n = 0, nmax = m_Entries.GetSize(); n < nmax; n++ )
  207.     {
  208.         CProcessInfo* p = (CProcessInfo*)m_Entries.GetAt(n);
  209.         PrintToFile(pFile, _T("\t<process>\r\n") );
  210.         PrintToFile(pFile, _T("\t\t<filename>%s</filename>\r\n"),(LPCTSTR)  XmlEscape(p->m_strFileName) );
  211.         PrintToFile(pFile, _T("\t\t<id>%s</id>\r\n"), (LPCTSTR) XmlEscape(p->m_strProcessID) );
  212.         PrintToFile(pFile, _T("\t\t<username>%s</username>\r\n"), (LPCTSTR) XmlEscape(p->m_strUsername) );
  213.         PrintToFile(pFile, _T("\t</process>\r\n") );
  214.     }
  215.  
  216.     PrintToFile(pFile, _T("</processlist>"));
  217. }
  218.  
  219. UINT CProcessList::GetContextMenuID()
  220. {
  221.     return IDR_CONTEXTMENU_PROCESSES;
  222. }
  223.  
  224.  
  225.