cs.lpszClass = AfxRegisterWndClass (0, NULL, NULL,
AfxGetApp()->LoadIcon (IDR_MAINFRAME));
return TRUE;
}
// –
void
CMainFrame: OnContextMenu (
CWnd * pWnd,
CPoint point
)
{
if (pWnd!= &m_wndView)
return;
int nSel = m_wndView. GetNextItem (-1, LVNI_SELECTED);
if (nSel == -1)
return;
if (point.x == -1 && point.y == -1)
{
RECT rect;
m_wndView. GetItemRect (nSel, &rect, LVIR_BOUNDS);
m_wndView. ClientToScreen(&rect);
point.x = rect.left + 1;
point.y = rect.bottom + 1;
}
CMenu menu;
int nMenu = m_bProcesses? 0: 1;
menu. GetSubMenu(nMenu)->TrackPopupMenu (TPM_LEFTALIGN|TPM_LEFTBUTTON,
point.x, point.y, this);
}
// –
// OnCreate
int
CMainFrame: OnCreate (
CREATESTRUCT * pCreateStruct
)
{
OnSysColorChange();
RECT rcEmpty;
SetRectEmpty(&rcEmpty);
if (CFrameWnd: OnCreate(pCreateStruct) == -1)
return -1;
// create status bar
UINT nInd = ID_SEPARATOR;
if (! m_ImageList. Create (16, 16, ILC_COLOR32|ILC_MASK, 16, 16))
return -1;
// insert the default application icon into the image list
m_ImageList. Add (LoadIcon(NULL, IDI_APPLICATION));
if (m_osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
m_hVdmDbg = LoadLibrary (_T(«vdmdbg.dll»));
if (m_hVdmDbg!= NULL)
{
m_pfnVDMEnumTaskWOWEx =
(VDMENUMTASKWOWEXPROC) GetProcAddress (m_hVdmDbg,
«VDMEnumTaskWOWEx»);
m_pfnVDMTerminateTaskWOW =
(VDMTERMINATETASKINWOWPROC) GetProcAddress (m_hVdmDbg,
«VDMTerminateTaskWOW»);
}
}
m_hPSAPI = LoadLibrary (_T(«psapi.dll»));
m_hPDH = LoadLibrary (_T(«pdh.dll»));
IWbemLocator * pLocator = NULL;
HRESULT hRes = CoCreateInstance (__uuidof(WbemLocator), NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IWbemLocator),
(PVOID *)&pLocator);
if (SUCCEEDED(hRes))
{
pLocator->Release();
m_bWmiAvailable = TRUE;
}
else
{
m_bWmiAvailable = FALSE;
}
m_bSedAvailable = CProcessSecInfo: IsDaclEditorAvailable();
m_nEnumProcMethod = (int) g_theApp. GetProfileInt (_T(«Settings»),
_T («EnumProcessesMethod»), ENUMPROCESSES_INVALID);
if (m_nEnumProcMethod == ENUMPROCESSES_PERFDATA &&
m_hPDH == NULL)
m_nEnumProcMethod = ENUMPROCESSES_INVALID;
if (m_nEnumProcMethod == ENUMPROCESSES_WMI &&
! m_bWmiAvailable)
m_nEnumProcMethod = ENUMPROCESSES_INVALID;
if (m_nEnumProcMethod == ENUMPROCESSES_INVALID)
{
if (m_osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
m_nEnumProcMethod = ENUMPROCESSES_TOOLHELP;
else
m_nEnumProcMethod = ENUMPROCESSES_NTAPI;
}
int bProcesses = (int) g_theApp. GetProfileInt (_T(«Settings»), _T («ProcessesMode»), 1);
m_nSortOrder = (int) g_theApp. GetProfileInt (_T(«Settings»),
_T («ProcessesSortOrder»), -1);
m_nAppsSortOrder = (int) g_theApp. GetProfileInt (_T(«Settings»),
_T («ApplicationsSortOrder»), -1);
m_bShow16Bit = (BOOL) g_theApp. GetProfileInt (_T(«Settings»),
_T («Show16Bit»), FALSE);
if (m_pfnVDMEnumTaskWOWEx == NULL ||
m_pfnVDMTerminateTaskWOW == NULL)
m_bShow16Bit = FALSE;
m_nRefreshPeriod = g_theApp. GetProfileInt (_T(«Settings»), _T («UpdateSpeed»), UPDATE_PERIOD_NORMAL);
if (m_nRefreshPeriod!= UPDATE_PERIOD_PAUSED)
SetTimer (1, m_nRefreshPeriod, NULL);
int bHung = (int) g_theApp. GetProfileInt (_T(«Settings»),_T («IsAppHung»), 0);
PBYTE pData = NULL;
UINT cbData = 0;
int * pnValues = NULL;
if (g_theApp. GetProfileBinary (_T(«Settings»), _T («ListView»),
&pData, &cbData))
{
if (cbData == 8 * sizeof(int))
{
pnValues = (int *) pData;
m_nProcColWidth[0] = pnValues[0];
m_nProcColWidth[1] = pnValues[1];
m_nAppsColWidth[0] = pnValues[2];
m_nAppsColWidth[1] = pnValues[3];
m_nProcColOrder[0] = pnValues[4];
m_nProcColOrder[1] = pnValues[5];
m_nAppsColOrder[0] = pnValues[6];
m_nAppsColOrder[1] = pnValues[7];
}
delete[] pData;
}
if (pnValues == NULL)
{
m_nProcColWidth[0] = LVSCW_AUTOSIZE_USEHEADER;
m_nProcColWidth[1] = LVSCW_AUTOSIZE_USEHEADER;
m_nAppsColWidth[0] = LVSCW_AUTOSIZE_USEHEADER;
m_nAppsColWidth[1] = LVSCW_AUTOSIZE_USEHEADER;
m_nProcColOrder[0] = 0;
m_nProcColOrder[1] = 1;
m_nAppsColOrder[0] = 0;
m_nAppsColOrder[1] = 1;
}
if (bProcesses)
OnViewProcesses();
else
OnViewApplications();
return 0;
}
// –
// OnDestroy
void
CMainFrame: OnDestroy()
{
SaveViewSettings();
g_theApp. WriteProfileInt (_T(«Settings»), _T («EnumProcessesMethod»),
m_nEnumProcMethod);
g_theApp. WriteProfileInt (_T(«Settings»), _T («ProcessesMode»),
m_bProcesses);
g_theApp. WriteProfileInt (_T(«Settings»), _T («ProcessesSortOrder»),
m_nSortOrder);
g_theApp. WriteProfileInt (_T(«Settings»), _T («ApplicationsSortOrder»),
m_nAppsSortOrder);
g_theApp. WriteProfileInt (_T(«Settings»), _T («Show16Bit»),
m_bShow16Bit);
g_theApp. WriteProfileInt (_T(«Settings»), _T («UpdateSpeed»),
m_nRefreshPeriod);
int nValues[8];
nValues[0] = m_nProcColWidth[0];
nValues[1] = m_nProcColWidth[1];
nValues[2] = m_nAppsColWidth[0];
nValues[3] = m_nAppsColWidth[1];
nValues[4] = m_nProcColOrder[0];
nValues[5] = m_nProcColOrder[1];
nValues[6] = m_nAppsColOrder[0];
nValues[7] = m_nAppsColOrder[1];
g_theApp. WriteProfileBinary (_T(«Settings»), _T («ListView»),
(LPBYTE) nValues, sizeof(nValues));
if (m_hVdmDbg!= NULL)
_VERIFY (FreeLibrary(m_hVdmDbg));
m_hVdmDbg = NULL;
m_pfnVDMEnumTaskWOWEx = NULL;
m_pfnVDMTerminateTaskWOW = NULL;
if (m_hPSAPI!= NULL)
_VERIFY (FreeLibrary(m_hPSAPI));
if (m_hPDH!= NULL)
_VERIFY (FreeLibrary(m_hPDH));
CFrameWnd: OnDestroy();
}
// –
// OnSetFocus
void
CMainFrame: OnSetFocus (
CWnd * pOldWnd
)
{
_UNUSED(pOldWnd);
// forward focus to the view window
m_wndView. SetFocus();
}
// –
// OnSysColorChange
void CMainFrame: OnSysColorChange()
{
CFrameWnd: OnSysColorChange();
if (m_bmSortUp.m_hObject!= NULL)
_VERIFY (m_bmSortUp. DeleteObject());
if (m_bmSortDown.m_hObject!= NULL)
_VERIFY (m_bmSortDown. DeleteObject());
_VERIFY (m_bmSortUp. LoadMappedBitmap (IDB_SORT_UP));
_VERIFY (m_bmSortDown. LoadMappedBitmap (IDB_SORT_DOWN));
}
// –
// OnTimer
void
CMainFrame: OnTimer (
UINT nIDEvent
)
{
_UNUSED(nIDEvent);
_ASSERTE (nIDEvent == 1);
OnViewRefresh();
}
// –
// OnActionSecurity
void
CMainFrame: OnActionSecurity()
{
int nSel = m_wndView. GetNextItem (-1, LVNI_SELECTED); _ASSERTE (nSel!= -1);
CItemData * pData = (CItemData *) m_wndView. GetItemData(nSel);
_ASSERTE (_CrtIsValidHeapPointer(pData));
CProcessSecInfo SecInfo;
if (! SecInfo. EditDacl (m_hWnd, pData->dwProcessId, pData->strName))
AfxThrowOleException (HRESULT_FROM_WIN32 (GetLastError()));
}
// –
// OnActionSecurity_Update
void
CMainFrame: OnActionSecurity_Update (CCmdUI * pCmdUI)
{
BOOL bEnable = FALSE;
if (m_bProcesses && m_bSedAvailable && m_strMachineName. IsEmpty())
{
int nSel = m_wndView. GetNextItem (-1, LVNI_SELECTED);
if (nSel!= -1)
{CItemData * pData = (CItemData *) m_wndView. GetItemData(nSel);
_ASSERTE (_CrtIsValidHeapPointer(pData));
Enable = pData->dwProcessId!= 0 && pData->dwWowTaskId == 0;
}
}
pCmdUI->Enable(bEnable);
}
// –
// OnViewApplications
void
CMainFrame: OnViewApplications()
{
if (m_bProcesses!= 0)
{
if (:IsWindow (m_wndView.m_hWnd))
{
SaveViewSettings();
m_wndView. DestroyWindow();
}
DWORD dwStyle;
// create a view to occupy the client area of the frame
dwStyle = WS_CHILD|WS_VISIBLE|LVS_REPORT|LVS_SHAREIMAGELISTS|
LVS_SHOWSELALWAYS|LVS_SINGLESEL;
RECT rcClient;
GetClientRect(&rcClient);
m_wndView. Create (dwStyle, rcClient, this, AFX_IDW_PANE_FIRST);
m_wndView. ModifyStyleEx (0, WS_EX_CLIENTEDGE);
m_wndView. SetExtendedStyle (LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP);
m_wndView. SetImageList (&m_ImageList, LVSIL_SMALL);
RecalcLayout();
m_strMachineName. Empty();
// remove any colums currently in the list
while (m_wndView. DeleteColumn(0));
TCHAR szColumn[256];
// add two colums to the list control
AfxLoadString (IDS_APPS_TASK, szColumn, countof(szColumn));
m_wndView. InsertColumn (0, szColumn);
AfxLoadString (IDS_APPS_STATUS, szColumn, countof(szColumn));
m_wndView. InsertColumn (1, szColumn);
m_bProcesses = 0;
m_wndView. SetRedraw(FALSE);
m_wndView. SetColumnWidth (0, m_nAppsColWidth[0]);
m_wndView. SetColumnWidth (1, m_nAppsColWidth[1]);
m_wndView. SetColumnOrderArray (2, m_nAppsColOrder);
OnViewRefresh();
}
}
// –
// OnViewApplications_Update
void
CMainFrame: OnViewApplications_Update (CCmdUI * pCmdUI)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->SetRadio (! m_bProcesses);
}
// –
// OnViewProcesses
void
CMainFrame: OnViewProcesses()
{
if (m_bProcesses!= 1)
{
if (:IsWindow (m_wndView.m_hWnd))
{
SaveViewSettings();
m_wndView. DestroyWindow();
}
DWORD dwStyle;
// create a view to occupy the client area of the frame
dwStyle = WS_CHILD|WS_VISIBLE|LVS_REPORT|LVS_SHAREIMAGELISTS|
LVS_SHOWSELALWAYS|LVS_SINGLESEL;
RECT rcClient;
GetClientRect(&rcClient);
m_wndView. Create (dwStyle, rcClient, this, AFX_IDW_PANE_FIRST);
m_wndView. ModifyStyleEx (0, WS_EX_CLIENTEDGE);
m_wndView. SetExtendedStyle (LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP);
RecalcLayout();
TCHAR szColumn[256];
// add two colums to the list control
AfxLoadString (IDS_PROCESSES_ID, szColumn, countof(szColumn));
m_wndView. InsertColumn (0, szColumn);
AfxLoadString (IDS_PROCESSES_NAME, szColumn, countof(szColumn));
m_wndView. InsertColumn (1, szColumn);
AfxLoadString (IDS_PROCESSES_time, szColumn, countof(szColumn));
m_wndView. InsertColumn (2, szColumn);
m_bProcesses = 1;
m_wndView. SetRedraw(FALSE);
m_wndView. SetColumnWidth (0, 103);
m_wndView. SetColumnWidth (1, 103);
m_wndView. SetColumnWidth (2, 103);
m_wndView. SetColumnOrderArray (2, m_nProcColOrder);
OnViewRefresh();
}
}
// –
// OnViewProcesses_Update
void CMainFrame: OnViewProcesses_Update (
CCmdUI * pCmdUI
)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->SetRadio (m_bProcesses);
}
// –
// OnViewRefresh
//
// Handles View|Refresh menu command. Refreshes the currently displayed
// information.
void
CMainFrame: OnViewRefresh()
{
int i;
if (m_nRefreshPeriod == UPDATE_PERIOD_PAUSED)
AfxGetApp()->BeginWaitCursor();
m_wndView. SetRedraw(FALSE);
// mark all items
int nCount = m_wndView. GetItemCount();
for (i = 0; i < nCount; i++)
{
CItemData * pData = (CItemData *) m_wndView. GetItemData(i);
_ASSERTE (pData!= NULL);
pData->bDelete = TRUE;
}
try
{
if (m_bProcesses)
ListProcesses();
}
catch (CException * pe)
{
if (m_nRefreshPeriod!= UPDATE_PERIOD_PAUSED)
{
KillTimer(1);
m_nRefreshPeriod = UPDATE_PERIOD_PAUSED;
}
pe->ReportError();
pe->Delete();
}
// walk through the list and delete items which are still marked
nCount = m_wndView. GetItemCount();
for (i = nCount – 1; i >= 0; i–)
{
CItemData * pData = (CItemData *) m_wndView. GetItemData(i);
_ASSERTE (pData!= NULL);
if (pData->bDelete)
m_wndView. DeleteItem(i);
}
m_wndView. SetRedraw(TRUE);
TCHAR szFormat[256];
TCHAR szTitle[256];
if (m_strMachineName. IsEmpty())
AfxLoadString (IDS_TITLE_LOCAL, szFormat, countof(szFormat));
else
AfxLoadString (IDS_TITLE_REMOTE, szFormat, countof(szFormat));
wsprintf (szTitle, szFormat, (LPCTSTR) m_strMachineName);
SetWindowText(szTitle);
if (m_nRefreshPeriod == UPDATE_PERIOD_PAUSED)
AfxGetApp()->EndWaitCursor();
}
// –
// OnViewSpeedPaused_Update
void
CMainFrame: OnViewSpeedPaused_Update (
CCmdUI * pCmdUI
)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->SetRadio (m_nRefreshPeriod == UPDATE_PERIOD_PAUSED);
}
// –
void
CMainFrame: OnOptionsEnumprocToolhelp()
{
m_nEnumProcMethod = ENUMPROCESSES_TOOLHELP;
OnViewRefresh();
}
// –
// OnOptionsEnumprocToolhelp_Update
void CMainFrame: OnOptionsEnumprocToolhelp_Update (CCmdUI* pCmdUI)
{
_ASSERTE (pCmdUI!= NULL);
BOOL bEnable = (m_osvi.dwPlatformId == VER_PLATFORM_WIN32_NT &&
m_osvi.dwMajorVersion >= 5) || m_osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS;
pCmdUI->Enable (bEnable && m_strMachineName. IsEmpty());
pCmdUI->SetRadio (m_nEnumProcMethod == ENUMPROCESSES_TOOLHELP);
}
// –
// OnOptionsEnumprocNtapi
void CMainFrame: OnOptionsEnumprocNtapi()
{
m_nEnumProcMethod = ENUMPROCESSES_NTAPI;
OnViewRefresh();
}
// –
// OnOptionsEnumprocNtapi_Update
void
CMainFrame: OnOptionsEnumprocNtapi_Update (
CCmdUI * pCmdUI
)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->Enable (m_osvi.dwPlatformId == VER_PLATFORM_WIN32_NT &&
m_strMachineName. IsEmpty());
pCmdUI->SetRadio (m_nEnumProcMethod == ENUMPROCESSES_NTAPI);
}
// –
// OnOptionsEnumprocPerfdata
void CMainFrame: OnOptionsEnumprocPerfdata()
{
m_nEnumProcMethod = ENUMPROCESSES_PERFDATA;
OnViewRefresh();
}
// –
// OnOptionsEnumprocPerfdata_Update
void
CMainFrame: OnOptionsEnumprocPerfdata_Update (
CCmdUI * pCmdUI
)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->Enable (m_hPDH!= NULL);
pCmdUI->SetRadio (m_nEnumProcMethod == ENUMPROCESSES_PERFDATA);
}
// –
// OnOptionsEnumprocWmi
void
CMainFrame: OnOptionsEnumprocWmi()
{
m_nEnumProcMethod = ENUMPROCESSES_WMI;
OnViewRefresh();
}
// –
// OnOptionsEnumprocWmi_Update
void
CMainFrame: OnOptionsEnumprocWmi_Update (
CCmdUI * pCmdUI
)
{
_ASSERTE (pCmdUI!= NULL);
pCmdUI->Enable (m_bWmiAvailable!= NULL);
pCmdUI->SetRadio (m_nEnumProcMethod == ENUMPROCESSES_WMI);
}
// –
// OnOptionsDebug
void
CMainFrame: OnOptionsDebug()
{
TOKEN_PRIVILEGES tkp;
tkp. PrivilegeCount = 1;
LookupPrivilegeValue (NULL, SE_DEBUG_NAME, &tkp. Privileges[0].Luid);
HANDLE hToken;
if (OpenProcessToken (GetCurrentProcess(), ЕOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,&hToken))
{
AdjustTokenPrivileges (hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL);
CloseHandle(hToken);
}
OnViewRefresh();
}
// –
// OnOptionsDebug_Update
void
CMainFrame: OnOptionsDebug_Update (CCmdUI* pCmdUI)
{
_ASSERTE (pCmdUI!= NULL);
LUID Luid;
LookupPrivilegeValue (NULL, SE_DEBUG_NAME, &Luid);
if (m_osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{}
else
pCmdUI->Enable(FALSE);
}
// –
// OnList_ColumnClick
void
CMainFrame: OnList_ColumnClick (
NMHDR * pNMHDR,
LRESULT * pResult
)
{
_UNUSED(pResult);
_ASSERTE (pNMHDR!= NULL);
_ASSERTE (pResult!= NULL);
NMLISTVIEW * pnmlv = (NMLISTVIEW *) pNMHDR;
if (m_bProcesses)
{
if (pnmlv->iSubItem == abs (m_nSortOrder) – 1)
m_nSortOrder = – m_nSortOrder;
else
m_nSortOrder = – (pnmlv->iSubItem + 1);
m_wndView. SortItems (SortCallback, m_nSortOrder);
SetSortMark (m_nSortOrder);
}
else
{
if (pnmlv->iSubItem == abs (m_nAppsSortOrder) – 1)
m_nAppsSortOrder = – m_nAppsSortOrder;
else
m_nAppsSortOrder = – (pnmlv->iSubItem + 1);
m_wndView. SortItems (AppsSortCallback, m_nAppsSortOrder);