?? pviewce.cpp
字號:
///////////////////////////////////////////////////////////////////////////////
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// This is "Sample Code" and is distributable subject to the terms of the
// end user license agreement.
///////////////////////////////////////////////////////////////////////////////
//
// PViewCE.CPP
//
// CE Process Viewer
//
#include <windows.h>
#include <aygshell.h>
#include <tpcshell.h>
#include <tlhelp32.h>
// Note: because this sample uses tlhelp32.h, it must be signed with a
// privileged certificate
#include <tlhelp32.h>
#include "newres.h"
#include "resource.h"
/*===========================================================================
* Macro Definitions
*/
#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
/*===========================================================================
* Type Definitions
*/
struct MODSNAP
{
DWORD dwOwnerPID;
HANDLE hSnapShot;
};
/*===========================================================================
* Function Prototypes
*/
BOOL CALLBACK MainDialogProc(
const HWND hDlg,
const UINT uiMessage,
const WPARAM wParam,
const LPARAM lParam
);
BOOL CALLBACK ProcessDialogProc(
const HWND hDlg,
const UINT uiMessage,
const WPARAM wParam,
const LPARAM lParam
);
BOOL CALLBACK ThreadDialogProc(
const HWND hDlg,
const UINT uiMessage,
const WPARAM wParam,
const LPARAM lParam
);
BOOL CALLBACK ModuleDialogProc(
const HWND hDlg,
const UINT uiMessage,
const WPARAM wParam,
const LPARAM lPara
);
static LPCTSTR StringFromResources(
UINT uStringID
);
static BOOL FillProcList(
HANDLE hProcessSnap
);
static BOOL FillThreadList(
HANDLE hProcessSnap,
DWORD dwOwnerPID
);
static BOOL FillModuleList(
DWORD dwOwnerPID
);
static BOOL InitModuleSnap(
HANDLE hProcessSnap);
static int PID2ArrayID(
DWORD dwOwnerPID
);
/*===========================================================================
* Global Variables
*/
HINSTANCE g_hInst = NULL;
// Variable for snapshot info
MODSNAP* g_pmsModuleSnap = NULL;
HANDLE g_hProcessSnap = NULL;
int g_iTotalProcs = 0;
HWND g_hWndMain;
HWND g_hWndListViews[3];
LPCTSTR pszAppName;
LPCTSTR pszTitle;
// define to index into g_hWndListViews
#define PROCESSLV 0
#define THREADLV 1
#define MODULELV 2
//Utility functions
// Purpose: Determine at runtime if the app is running on a smartphone device
static BOOL IsSmartphone()
{
TCHAR tszPlatform[64];
if (TRUE == SystemParametersInfo(SPI_GETPLATFORMTYPE,
sizeof(tszPlatform)/sizeof(*tszPlatform),tszPlatform,0))
{
if (0 == _tcsicmp(TEXT("Smartphone"), tszPlatform))
{
return TRUE;
}
}
return FALSE;
}
int MyMessageBox(HWND hWnd, LPCTSTR lpctszMessage)
{
return MessageBox(hWnd, lpctszMessage, TEXT("PViewCE"), MB_OK);
}
static LPCTSTR StringFromResources(
UINT uStringID
)
{
// NOTE: Passing NULL for the 3rd parameter of LoadString causes it to
// return a pointer to the string in the resource file. It requires that
// the resource file is compiled with the "/n" switch (see SDK docs).
return (LPCTSTR) LoadString(g_hInst, uStringID, NULL, 0);
}
DWORD GetListViewProc()
{
DWORD dwReturn = NULL;
LVITEM lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = ListView_GetSelectionMark(g_hWndListViews[PROCESSLV]);
if (-1 != lvi.iItem)
{
lvi.mask = LVIF_PARAM;
ListView_GetItem(g_hWndListViews[PROCESSLV], &lvi);
dwReturn = lvi.lParam;
}
return dwReturn;
}
DWORD GetListViewThread()
{
DWORD dwReturn = NULL;
LVITEM lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = ListView_GetSelectionMark(g_hWndListViews[THREADLV]);
if (-1 != lvi.iItem)
{
lvi.mask = LVIF_PARAM;
ListView_GetItem(g_hWndListViews[THREADLV], &lvi);
dwReturn = lvi.lParam;
}
return dwReturn;
}
DWORD GetListViewModule()
{
DWORD dwReturn = NULL;
LVITEM lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = ListView_GetSelectionMark(g_hWndListViews[MODULELV]);
if (-1 != lvi.iItem)
{
lvi.mask = LVIF_PARAM;
ListView_GetItem(g_hWndListViews[MODULELV], &lvi);
dwReturn = lvi.lParam;
}
return dwReturn;
}
//Listbox management functions
BOOL InitListViews(
HWND hWndParent
)
{
LVCOLUMN lvc;
RECT rc;
// Create each ListView and configure it
GetClientRect(hWndParent, &rc);
for (int x = 0; x < ARRAY_LENGTH(g_hWndListViews); x++)
{
g_hWndListViews[x] = CreateWindow(WC_LISTVIEW, TEXT("ListItem"),
WS_CHILD | LVS_REPORT | LVS_SORTASCENDING | WS_VISIBLE | WS_HSCROLL | LVS_SINGLESEL,
0, 0, rc.right, rc.bottom, hWndParent, NULL, g_hInst, NULL);
if (NULL == g_hWndListViews[x])
{
return FALSE;
}
ListView_SetExtendedListViewStyle(g_hWndListViews[x], LVS_EX_FULLROWSELECT);
}
// Now setup the ListView headers
//Find out how big to make the right column (TID/PID/MID)
int iIdWidth = 90;
//int iIdWidth = SendMessage(g_hWndListViews[PROCESSLV], LVM_GETSTRINGWIDTH, 0, (LPARAM)TEXT("0x00000000"));
// Configure PROCESSLV first
ZeroMemory(&lvc, sizeof(lvc));
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvc.cx = (rc.right - rc.left) - iIdWidth;
// Add column for Process
lvc.pszText = (LPTSTR)StringFromResources(IDS_PROCESS);
ListView_InsertColumn(g_hWndListViews[PROCESSLV], lvc.iSubItem, &lvc);
// Add column for PID
lvc.iSubItem++;
lvc.pszText = (LPTSTR)StringFromResources(IDS_PID);
lvc.cx = iIdWidth;
ListView_InsertColumn(g_hWndListViews[PROCESSLV], lvc.iSubItem, &lvc);
// Configure THREADLV
ZeroMemory(&lvc, sizeof(lvc));
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvc.cx = (rc.right - rc.left) - iIdWidth;
// Add column for Process
lvc.pszText = (LPTSTR)StringFromResources(IDS_TID);
ListView_InsertColumn(g_hWndListViews[THREADLV], lvc.iSubItem, &lvc);
// Add column for PID
lvc.iSubItem++;
lvc.pszText = (LPTSTR)StringFromResources(IDS_BASEPRI);
lvc.cx = iIdWidth;
ListView_InsertColumn(g_hWndListViews[THREADLV], lvc.iSubItem, &lvc);
// Configure MODULELV
ZeroMemory(&lvc, sizeof(lvc));
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvc.cx = (rc.right - rc.left) - iIdWidth;
// Add column for Process
lvc.pszText = (LPTSTR)StringFromResources(IDS_MODULE);
ListView_InsertColumn(g_hWndListViews[MODULELV], lvc.iSubItem, &lvc);
// Add column for PID
lvc.iSubItem++;
lvc.pszText = (LPTSTR)StringFromResources(IDS_MID);
lvc.cx = iIdWidth;
ListView_InsertColumn(g_hWndListViews[MODULELV], lvc.iSubItem, &lvc);
return TRUE;
}
BOOL GetSnapshots()
{
// Check for old snapshot
if (NULL != g_hProcessSnap)
{
VERIFY(CloseToolhelp32Snapshot(g_hProcessSnap));
}
if (NULL != g_pmsModuleSnap)
{
free(g_pmsModuleSnap);
}
// Create a new snapshot
g_hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
if (INVALID_HANDLE_VALUE == g_hProcessSnap)
{
MyMessageBox(NULL, StringFromResources(IDS_CREATESNAPSHOT_FAILED));
return FALSE;
}
// Take all the module snapshots - if any of them fails, exit...
// This will make the snapshot operation as a whole seem atomic
if (FALSE == InitModuleSnap(g_hProcessSnap))
{
return FALSE;
}
return TRUE;
}
void CleanupListViewes()
{
for (int x = 0; x < ARRAY_LENGTH(g_hWndListViews); x++)
{
if (TRUE == IsWindow(g_hWndListViews[x]))
{
DestroyWindow(g_hWndListViews[x]);
}
}
}
BOOL InitWindow(
HWND hWnd
)
{
SHMENUBARINFO mbi;
// fill in the menubar info struct
ZeroMemory(&mbi, sizeof(mbi));
mbi.cbSize = sizeof(mbi);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU_BAR;
mbi.hInstRes = g_hInst;
return SHCreateMenuBar(&mbi);
}
LRESULT CALLBACK MainWndProc(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch (Msg)
{
case WM_CREATE:
if (FALSE == InitWindow(hWnd))
{
MyMessageBox(NULL, TEXT("Failed to init window"));
//Failed to init window, lets quit
PostQuitMessage(0);
}
if (FALSE == InitListViews(hWnd))
{
MyMessageBox(NULL, TEXT("Failed to init window"));
//Failed init, lets quit
PostQuitMessage(0);
}
PostMessage(hWnd, WM_COMMAND, IDM_MENU_REFRESH, NULL);
break;
case WM_COMMAND:
switch (wParam)
{
case IDM_MENU_PROCESS:
BringWindowToTop(g_hWndListViews[PROCESSLV]);
SetFocus(g_hWndListViews[PROCESSLV]);
break;
case IDM_MENU_THREAD:
FillThreadList(g_hProcessSnap, GetListViewProc());
BringWindowToTop(g_hWndListViews[THREADLV]);
SetFocus(g_hWndListViews[THREADLV]);
break;
case IDM_MENU_MODULE:
FillModuleList(GetListViewProc());
BringWindowToTop(g_hWndListViews[MODULELV]);
SetFocus(g_hWndListViews[MODULELV]);
break;
case IDM_MENU_REFRESH:
GetSnapshots();
FillProcList(g_hProcessSnap);
BringWindowToTop(g_hWndListViews[PROCESSLV]);
SetFocus(g_hWndListViews[PROCESSLV]);
break;
case IDM_MENU_QUIT:
PostMessage(hWnd, WM_DESTROY, NULL, NULL);
break;
case IDM_MENU_SOFT1:
{
HWND hWndFocus = GetFocus();
if (hWndFocus == g_hWndListViews[PROCESSLV])
{
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DLGPROCESS),
0, (DLGPROC)ProcessDialogProc);
SetFocus(g_hWndListViews[PROCESSLV]);
}
else if (hWndFocus == g_hWndListViews[THREADLV])
{
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DLGTHREAD),
0, (DLGPROC)ThreadDialogProc);
SetFocus(g_hWndListViews[THREADLV]);
}
else if (hWndFocus == g_hWndListViews[MODULELV])
{
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DLGMODULE),
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -