SetSortMark (m_nAppsSortOrder);
}
}
// –
// OnList_DeleteItem
void
CMainFrame: OnList_DeleteItem (NMHDR * pNMHDR, LRESULT * pResult)
{
_UNUSED(pResult);
_ASSERTE (pNMHDR!= NULL);
_ASSERTE (pResult!= NULL);
NMLISTVIEW * pnmlv = (NMLISTVIEW *) pNMHDR;
CItemData * pData = (CItemData *) m_wndView. GetItemData (pnmlv->iItem);
_ASSERTE (pData!= NULL);
delete pData;
}
// –
// SaveViewSettings
void
CMainFrame: SaveViewSettings()
{
if (m_bProcesses)
{
m_nProcColWidth[0] = m_wndView. GetColumnWidth(0);
m_nProcColWidth[1] = m_wndView. GetColumnWidth(1);
m_nProcColWidth[2] = m_wndView. GetColumnWidth(2);
m_wndView. GetColumnOrderArray (m_nProcColOrder, 2);
}
else
{
m_nAppsColWidth[0] = m_wndView. GetColumnWidth(0);
m_nAppsColWidth[1] = m_wndView. GetColumnWidth(1);
m_wndView. GetColumnOrderArray (m_nAppsColOrder, 2);
}
}
// –
// SetSortMark
void
CMainFrame: SetSortMark (int nOrder)
{
int nSubItem = abs(nOrder) – 1;
CHeaderCtrl& hdr = *m_wndView. GetHeaderCtrl();
int nCount = hdr. GetItemCount();
for (int i = 0; i < nCount; i++)
{
HDITEM hdi;
hdi.mask = HDI_FORMAT;
_VERIFY (hdr. GetItem (i, &hdi));
if (i!= nSubItem)
{
hdi.fmt &= ~(HDF_BITMAP|HDF_BITMAP_ON_RIGHT);
}
else
{
hdi.mask |= HDI_BITMAP;
hdi.fmt |= HDF_BITMAP|HDF_BITMAP_ON_RIGHT;
hdi.hbm = (nOrder > 0)? m_bmSortDown: m_bmSortUp;
}
_VERIFY (hdr. SetItem (i, &hdi));
}
}
// –
// SortCallback
int
CALLBACK
CMainFrame: SortCallback (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CItemData * pData1 = (CItemData *) lParam1;
CItemData * pData2 = (CItemData *) lParam2;
_ASSERTE (_CrtIsValidHeapPointer(pData1));
_ASSERTE (_CrtIsValidHeapPointer(pData2));
int nRes;
switch (abs(lParamSort))
{
// sort on process identifiers
case 1:
nRes = (int) pData2->dwProcessId – (int) pData1->dwProcessId;
break;
// sort on process names
case 2:
nRes = lstrcmpi (pData2->strName, pData1->strName);
break;
default:
_ASSERTE(0);
__assume(0);
}
if (lParamSort < 0)
nRes = – nRes;
if (nRes == 0)
nRes = (int) pData1->dwWowTaskId – (int) pData2->dwWowTaskId;
return nRes;
}
// –
// AppsSortCallback
int
CALLBACK
CMainFrame: AppsSortCallback (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CItemData * pData1 = (CItemData *) lParam1;
CItemData * pData2 = (CItemData *) lParam2;
_ASSERTE (_CrtIsValidHeapPointer(pData1));
_ASSERTE (_CrtIsValidHeapPointer(pData2));
int nRes;
switch (abs(lParamSort))
{
// sort on application names
case 1:
nRes = lstrcmpi (pData2->strName, pData1->strName);
break;
// sort on application status
case 2:
if (pData1->bRunning &&! pData2->bRunning)
nRes = 1;
else if (! pData1->bRunning && pData2->bRunning)
nRes = -1;
else
nRes = lstrcmpi (pData2->strName, pData1->strName);
break;
default:
_ASSERTE(0);
__assume(0);
}
if (lParamSort < 0)
nRes = – nRes;
return nRes;
}
// –
// ListProcesses
void
CMainFrame: ListProcesses()
{
PFNENUMPROCESSES pfnEnumProc = NULL;
// select enumeration function to use
switch (m_nEnumProcMethod)
{
case ENUMPROCESSES_TOOLHELP:
pfnEnumProc = EnumProcesses_ToolHelp;
break;
case ENUMPROCESSES_NTAPI:
pfnEnumProc = EnumProcesses_NtApi;
break;
case ENUMPROCESSES_PERFDATA:
pfnEnumProc = EnumProcesses_PerfData;
break;
case ENUMPROCESSES_WMI:
pfnEnumProc = EnumProcesses_Wmi;
break;
default:
_ASSERTE(0);
__assume(0);
}
_ASSERTE (pfnEnumProc!= NULL);
LPCTSTR pszMachineName = NULL;
if (! m_strMachineName. IsEmpty())
pszMachineName = m_strMachineName;
// call process enumeration function
if (! pfnEnumProc (pszMachineName, EnumProcessCallback, (LPARAM) this))
AfxThrowOleException (HRESULT_FROM_WIN32 (GetLastError()));
m_wndView. SortItems (SortCallback, m_nSortOrder);
SetSortMark (m_nSortOrder);
}
// –
// EnumProcessCallback
BOOL
CALLBACK
CMainFrame: EnumProcessCallback (
DWORD dwProcessId,
LPCTSTR pszName,
char CreateTime_str[55],
LPARAM lParam
)
{
int nItem;
CMainFrame * pFrame = (CMainFrame *) lParam;
ASSERT_VALID(pFrame);
CListCtrl& wndView = pFrame->m_wndView;
CItemData * pData;
BOOL bFound = FALSE;
TCHAR szID[64];
wsprintf (szID, _T («%u (0x % X)»), dwProcessId, dwProcessId);
// try to find the corresponding item in the list
int nCount = wndView. GetItemCount();
for (nItem = 0; nItem < nCount; nItem++)
{
pData = (CItemData *) wndView. GetItemData(nItem);
if (pData->dwProcessId == dwProcessId &&
pData->dwWowTaskId == 0 &&
pData->strName == pszName)
{
pData->bDelete = FALSE;
bFound = TRUE;
break;
}
}
if (! bFound)
{
pData = new CItemData (dwProcessId, DWORD(0));
nItem = wndView. InsertItem (LVIF_TEXT|LVIF_PARAM, nCount, szID,
0, 0, -1, (LPARAM) pData);
if (nItem == -1)
{
delete pData;
return TRUE;
}
TCHAR szUnavailable[256];
if (pszName == NULL)
{
AfxLoadString (IDS_NAME_UNAVAILABLE, szUnavailable, countof(szUnavailable));
pszName = szUnavailable;
}
pData->strName = pszName;
wndView. SetItemText (nItem, 1, pszName);
wndView. SetItemText (nItem, 2, CreateTime_str);
}
if (pFrame->m_bShow16Bit &&
(lstrcmpi (pszName, _T («NTVDM.EXE»)) == 0 ||
lstrcmpi (pszName, _T («NTVDM»)) == 0))
{
_ASSERTE (pFrame->m_pfnVDMEnumTaskWOWEx!= NULL);
pFrame->m_dwProcessId = dwProcessId;
}
return TRUE;
}
void CMainFrame: OnAbout()
{
DestroyWindow();
}