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 >
Wrap
C/C++ Source or Header
|
2005-01-05
|
8KB
|
225 lines
#include "stdafx.h"
#include "resource.h"
#include "CProcessList.h"
#include "CConfiguration.h"
#include "Aclapi.h"
#include "psapi.H"
#include "2kwrapper.h"
static CString strThisProcessUser;
CString CProcessInfo::GetDisplayString(DWORD dwIndex)
{
switch(dwIndex)
{
case 0: return m_strFileName;
case 1: return m_strProcessID;
case 2: return m_strUsername;
case 3: return m_strPageFaultCount;
case 4: return m_strPeakWorkingSetSize;
case 5: return m_strWorkingSetSize;
case 6: return m_strQuotaPeakPagedPoolUsage;
case 7: return m_strQuotaPagedPoolUsage;
case 8: return m_strQuotaPeakNonPagedPoolUsage;
case 9: return m_strQuotaNonPagedPoolUsage;
case 10: return m_strPagefileUsage;
case 11: return m_strPeakPagefileUsage;
}
return CString();
}
COLORREF CProcessInfo::GetTextColor()
{
return m_dwColor;
}
CString CProcessInfo::GetInfoTip()
{
return m_strFileName;
}
CProcessInfo::CProcessInfo( HANDLE hProcess, DWORD dwProcessID, HMODULE* lphModules, DWORD dwModuleCount )
: m_dwProcessID( dwProcessID )
{
m_strProcessID.Format(_T("%ld"), dwProcessID );
TCHAR szFileName[1024];
for( DWORD dwIndex = 0; dwIndex < dwModuleCount; dwIndex++ )
{
if( GetModuleFileNameEx( hProcess, lphModules[dwIndex], szFileName, sizeof(szFileName) ) )
{
if( (lpfnGetLongPathName != NULL) &&
(tstrchr(szFileName,'~') != NULL) )
{
TCHAR szLongPath[1024];
if( lpfnGetLongPathName(szFileName,szLongPath,sizeof(szLongPath)) )
{
lstrcpy(szFileName,szLongPath);
}
}
if( !dwIndex )
{
m_strFileName = szFileName;
}
else break;
//m_Modules.AddTail( new ModuleInformation( szFileName ) );
}
}
m_strUsername = GetUserNameFromProcess(hProcess);
m_dwColor = (m_strUsername.CompareNoCase( strThisProcessUser ) == 0) ? RGB(0,0,0) : RGB(128, 128, 128);
ZeroMemory( &m_mc, sizeof(m_mc));
if( GetProcessMemoryInfo(hProcess, (PPROCESS_MEMORY_COUNTERS) szFileName, sizeof(szFileName) ) )
{
PPROCESS_MEMORY_COUNTERS pmc = PPROCESS_MEMORY_COUNTERS(szFileName);
m_mc = *pmc;
m_strPageFaultCount.Format(_T("%ld"), pmc->PageFaultCount );
m_strPeakWorkingSetSize.Format(_T("%ld"), pmc->PeakWorkingSetSize );
m_strWorkingSetSize.Format(_T("%ld"), pmc->WorkingSetSize );
m_strQuotaPeakPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPeakPagedPoolUsage );;
m_strQuotaPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPagedPoolUsage );
m_strQuotaPeakNonPagedPoolUsage.Format(_T("%ld"), pmc->QuotaPeakNonPagedPoolUsage );
m_strQuotaNonPagedPoolUsage.Format(_T("%ld"), pmc->QuotaNonPagedPoolUsage );
m_strPagefileUsage.Format(_T("%ld"), pmc->PagefileUsage );
m_strPeakPagefileUsage.Format(_T("%ld"), pmc->PeakPagefileUsage );
}
}
#define PROCESS_SORT_STRING_METHOD(NAME) \
static int SortBy##NAME(const CProcessInfo** ps1, const CProcessInfo** ps2) \
{ \
return (*ps1)->m_str##NAME.CompareNoCase((*ps2)->m_str##NAME); \
}
PROCESS_SORT_STRING_METHOD(Username)
PROCESS_SORT_STRING_METHOD(FileName)
#define PROCESS_SORT_MEMORY_COUNTERS(NAME) \
static int SortBy##NAME(const CProcessInfo** ps1, const CProcessInfo** ps2) \
{ \
return LONG((*ps1)->m_mc.NAME)-LONG((*ps2)->m_mc.NAME); \
}
static int SortByProcessID(const CProcessInfo** ps1, const CProcessInfo** ps2)
{
return LONG((*ps1)->m_dwProcessID)-LONG((*ps2)->m_dwProcessID);
}
PROCESS_SORT_MEMORY_COUNTERS(PageFaultCount)
PROCESS_SORT_MEMORY_COUNTERS(PeakWorkingSetSize)
PROCESS_SORT_MEMORY_COUNTERS(WorkingSetSize)
PROCESS_SORT_MEMORY_COUNTERS(QuotaPeakPagedPoolUsage)
PROCESS_SORT_MEMORY_COUNTERS(QuotaPagedPoolUsage)
PROCESS_SORT_MEMORY_COUNTERS(QuotaPeakNonPagedPoolUsage)
PROCESS_SORT_MEMORY_COUNTERS(QuotaNonPagedPoolUsage)
PROCESS_SORT_MEMORY_COUNTERS(PagefileUsage)
PROCESS_SORT_MEMORY_COUNTERS(PeakPagefileUsage)
static GENERICCOMPAREFN SortMethods[] = {
GENERICCOMPAREFN(&SortByFileName),
GENERICCOMPAREFN(&SortByProcessID),
GENERICCOMPAREFN(&SortByUsername),
GENERICCOMPAREFN(&SortByPageFaultCount),
GENERICCOMPAREFN(&SortByPeakWorkingSetSize),
GENERICCOMPAREFN(&SortByWorkingSetSize),
GENERICCOMPAREFN(&SortByQuotaPeakPagedPoolUsage),
GENERICCOMPAREFN(&SortByQuotaPagedPoolUsage),
GENERICCOMPAREFN(&SortByQuotaPeakNonPagedPoolUsage),
GENERICCOMPAREFN(&SortByQuotaNonPagedPoolUsage),
GENERICCOMPAREFN(&SortByPagefileUsage),
GENERICCOMPAREFN(&SortByPeakPagefileUsage),
};
CProcessList::CProcessList()
: CListViewEntries(SortMethods, _T("ProcessList"))
{
CreateColumns( _T("Process"), _T("PID"), _T("Username"),
_T("PageFaultCount"),
_T("PeakWorkingSetSize"),
_T("WorkingSetSize"),
_T("QuotaPeakPagedPoolUsage"),
_T("QuotaPagedPoolUsage"),
_T("QuotaPeakNonPagedPoolUsage"),
_T("QuotaNonPagedPoolUsage"),
_T("PagefileUsage"),
_T("PeakPagefileUsage"), NULL );
m_strTitle.Format( TEXT("pserv ") CURRENT_VERSION TEXT(": processes on local machine") );
strThisProcessUser = GetUserNameFromProcess(GetCurrentProcess());
}
BOOL CProcessList::Refresh()
{
DWORD dwArrayOfProcessIds[1024], dwNeeded;
DeleteObjects(m_Entries);
if( !EnumProcesses( dwArrayOfProcessIds, sizeof(dwArrayOfProcessIds), &dwNeeded ) )
return FALSE;
// Calculate how many process IDs were returned
DWORD dwCount = dwNeeded / sizeof(DWORD);
for( DWORD dwIndex = 0; dwIndex < dwCount; dwIndex++ )
{
DWORD dwProcessID = dwArrayOfProcessIds[dwIndex];
if( dwProcessID > 2 )
{
HANDLE hProcess = OpenProcess( PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwProcessID );
if( hProcess )
{
HMODULE hArray[1024];
DWORD dwNeeded, dwCount;
if( EnumProcessModules(hProcess,hArray,sizeof(hArray),&dwNeeded ) )
{
dwCount = dwNeeded / sizeof(HMODULE);
m_Entries.Add( new CProcessInfo( hProcess, dwProcessID, hArray, dwCount ) );
}
CloseHandle( hProcess );
}
}
}
return TRUE;
}
void CProcessList::ConnectTo( LPCTSTR lpszMachine )
{
// TODO: display error msg here ....
//m_strServiceMachine = lpszMachine;
}
void CProcessList::ExportXmlToFile( CFile* pFile )
{
PrintToFile(pFile, _T("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\r\n")
_T("<processlist>\r\n") );
for(int n = 0, nmax = m_Entries.GetSize(); n < nmax; n++ )
{
CProcessInfo* p = (CProcessInfo*)m_Entries.GetAt(n);
PrintToFile(pFile, _T("\t<process>\r\n") );
PrintToFile(pFile, _T("\t\t<filename>%s</filename>\r\n"),(LPCTSTR) XmlEscape(p->m_strFileName) );
PrintToFile(pFile, _T("\t\t<id>%s</id>\r\n"), (LPCTSTR) XmlEscape(p->m_strProcessID) );
PrintToFile(pFile, _T("\t\t<username>%s</username>\r\n"), (LPCTSTR) XmlEscape(p->m_strUsername) );
PrintToFile(pFile, _T("\t</process>\r\n") );
}
PrintToFile(pFile, _T("</processlist>"));
}
UINT CProcessList::GetContextMenuID()
{
return IDR_CONTEXTMENU_PROCESSES;
}