⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwalk.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 3 页
字号:

#include "pwalk.h"

#define strtok        My_mbstok

#define MAX_DRIVES    26    /* maximum number of logical drives */

/* system constants used externally */
int		    xChar,
		    yChar,
		    xScreen,
		    yScreen,
		    yFrame,
		    xFrame,
		    yCaption,
		    xVScrollBar;

char		szCaptionText[] ="Address\0State\0Prot\0Size\0BaseAddr\0Object\0Section\0Name\0";
char		szFormat[] = "%08lX~%s ~%s ~%lu~%08lX~%s ~%s ~%s ~";
SIZE		sChar0;
char		szFormatPages[] = "%05lX~%s ~%s ~%lu~%05lX~%s ~%s ~%s ~";
int			taColumns[] = {TA_RIGHT,  TA_LEFT, TA_LEFT, TA_RIGHT,
							TA_RIGHT, TA_RIGHT, TA_LEFT,  TA_LEFT,
							};
int			xColumns[]  = {      8,       9,      17,       31,
								40,      46,      47,       55,
							};
BOOL        bNumbersAsBytes = TRUE;
HFONT       hFontVar;

char        szFilePath[MAX_PATH] = "";
char        szFilename[MAX_PATH] = "";
HFONT       hFont;
LPVOID      lpWalkerList = NULL;
int         *Objects;
int		     nSortType = IDM_SORTADDRESS;
HWND		     hWndSysStat, hWndProStat, hInitDlg, hMemWnd;
HANDLE		     hChildEvents[nDEBUGEVENTS];
DBGPROCESS	     *lpChildProcess = NULL;
HMENU		     hPopup[MENUPOPUPS];
char		     szCurPath[MAX_PATH];


/* local functions */
BOOL   WINAPI InitEnvironment (HANDLE, int, char *);
void   WINAPI InitMenu (HWND);
void   WINAPI DrawListItem (DRAWITEMSTRUCT *);
int    WINAPI MakeVMQString (int, char *);
DWORD  WINAPI VMExceptionFilter (EXCEPTION_POINTERS *);
void   WINAPI SortList (HWND, int);
BOOL   WINAPI ViewableMemorySelection (HWND);
static void TextOutFields (HDC, int, LPRECT, LPSTR);


/****************************************************************************
    My_mbschr:  strchr() DBCS version
****************************************************************************/
unsigned char * _CRTAPI1 My_mbschr(
    unsigned char *psz, unsigned short uiSep)
{
    while (*psz != '\0' && *psz != uiSep) {
        psz = CharNext(psz);
    }
    if (*psz == '\0' && uiSep != '\0') {
        return NULL;
    } else {
        return psz;
    }
}
/****************************************************************************
    My_mbstok:  strtok() DBCS version
****************************************************************************/
unsigned char * _CRTAPI1 My_mbstok(
    unsigned char *pszSrc, unsigned char *pszSep)
{
    static char *pszSave = NULL;
    char *pszHead;
    char *psz;

    if (pszSrc == NULL) {
        if (pszSave == NULL) {
            return NULL;
        } else {
            psz = pszSave;
        }
    } else {
        psz = pszSrc;
    }

    /*********************************************/
    /* Skip delimiters to find a head of a token */
    /*********************************************/
    while (*psz) {
        if (IsDBCSLeadByte(*psz)) {
            break;
        } else if (NULL == My_mbschr(pszSep, *psz)) {
            break;
        }
        psz++;
    }
    if (*psz == '\0') {
        //No more token
        return (pszSave = NULL);
    }
    pszHead = psz;

    /******************************/
    /* Search a Tail of the token */
    /******************************/
    while (*psz) {
        if (IsDBCSLeadByte(*psz)) {
            psz += 2;
            continue;
        } else if (NULL != My_mbschr(pszSep, *psz)) {
            break;
        }
        psz++;
    }
    if (*psz == '\0') {
        pszSave = NULL;
    } else {
        //Found next delimiter
        pszSave = psz + 1;
        *psz = '\0';
    }
    return pszHead;
}


/* entry point of this executable */
int WINAPI WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
    HINSTANCE hInstance;
    HINSTANCE hPrevInstance;
    LPSTR     lpCmdLine;
    int       nCmdShow;
{
    MSG      msg;
    char     *lpszCmdLine = NULL;
    char     *lpCL;
    BOOL     bSwitch;

    /* set current path for use later */
    GetCurrentDirectory (MAX_PATH, szCurPath);

    // parse and copy command line parameters to local memory
    lpCL = GetCommandLine ();
    if (lpszCmdLine = (char *)LocalAlloc (LPTR, strlen (lpCL) + 1))
	GetCmdLine (lpCL, lpszCmdLine, &bSwitch);

    /* start window environment */
    if (!InitEnvironment (hInstance, nCmdShow, IsValidFile (lpszCmdLine) ? lpszCmdLine : NULL))
	return FALSE;

    /* free memory allocated for lpCmdLine */
    if (lpszCmdLine)
	LocalFree ((HLOCAL)lpszCmdLine);

    /* main window message loop */
    while (GetMessage (&msg, NULL, 0, 0))
	{
	TranslateMessage (&msg);
	DispatchMessage (&msg);
	}

    /* return success of application */
    return TRUE;
}



/*  start app */
BOOL WINAPI InitEnvironment (
    HANDLE    hInstance,
    int       nCmdShow,
    char      *lpszCmdLine)
    {
    WNDCLASS   wc;
    char       szClass[MAX_PATH];
    char       szTitle[MAX_PATH];
    char       szFilename[MAX_PATH];
    HWND       hWnd;


    /* register system statistics window class */
    LoadString (hInstance, IDS_SYSSTATCLASS, szClass, sizeof (szClass));
    wc.style	     = 0;
    wc.lpfnWndProc   = (WNDPROC)SysStatWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon	     = LoadIcon (hInstance, MAKEINTRESOURCE (IDR_SYSSTATICON));
    wc.hCursor	     = LoadCursor (0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = szClass;
    if (!RegisterClass (&wc) )
	return FALSE;

    /* register process statistics window class */
    LoadString (hInstance, IDS_PROSTATCLASS, szClass, sizeof (szClass));
    wc.style	     = 0;
    wc.lpfnWndProc   = (WNDPROC)ProStatWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon	     = LoadIcon (hInstance, MAKEINTRESOURCE (IDR_PROSTATICON));
    wc.hCursor	     = LoadCursor (0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = szClass;
    if (!RegisterClass (&wc) )
	return FALSE;

    /* register the status bar window class */
    LoadString (hInstance, IDS_STATUSCLASS, szClass, sizeof (szClass));
    wc.style	     = 0;
    wc.lpfnWndProc   = (WNDPROC)StatusWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = STATUSWXB;
    wc.hInstance     = hInstance;
    wc.hIcon	     = (HICON)NULL;
    wc.hCursor	     = LoadCursor (0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = szClass;
    if (!RegisterClass (&wc) )
	return FALSE;

    /* register the main frame window class */
    LoadString (hInstance, IDS_MEMVIEWCLASS, szClass, sizeof (szClass));
    wc.style	     = 0;
    wc.lpfnWndProc   = (WNDPROC)MemWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = VIEWWXB;
    wc.hInstance     = hInstance;
    wc.hIcon	     = LoadIcon (hInstance, MAKEINTRESOURCE (IDR_MAINICON));
    wc.hCursor	     = LoadCursor (0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = szClass;
    if (!RegisterClass (&wc) )
	return FALSE;

    /* register the main frame window class */
    LoadString (hInstance, IDS_WALKERCLASS, szClass, sizeof (szClass));
    wc.style	     = 0;
    wc.lpfnWndProc   = (WNDPROC)WalkerWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon	     = LoadIcon (hInstance, MAKEINTRESOURCE (IDR_MAINICON));
    wc.hCursor	     = LoadCursor (0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE (IDR_WALKERMENU);
    wc.lpszClassName = szClass;
    if (!RegisterClass (&wc) )
	return FALSE;

    /* create window caption */
    LoadString (hInstance, IDS_CAPTION, szTitle, sizeof (szTitle));
    if (lpszCmdLine != NULL &&
	((lpChildProcess = StartChildProcess (NULL, lpszCmdLine, hChildEvents)) != NULL))
	GetFileFromPath (lpszCmdLine, szFilename);
    else
	LoadString (hInstance, IDS_SELF, szFilename, MAX_PATH);
    strcat (szTitle, szFilename);

    /* create main frame window */
    hWnd = CreateWindow (szClass,
			 szTitle,
			 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
			 CW_USEDEFAULT,
			 0,
			 CW_USEDEFAULT,
			 0,
			 NULL,
			 NULL,
			 hInstance,
			 NULL);

    /* update parent window handle in child process information structure */
    if (lpChildProcess != NULL)
	lpChildProcess->hWnd = hWnd;

    if (!hWnd)
	return 0;

    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);
    return TRUE;
}


/* main window procedure */
LONG WINAPI WalkerWndProc (
    HWND    hWnd,
    UINT    uMsg,
    WPARAM  wParam,
    LPARAM  lParam)
{
       LONG    lRet = 1;

static LOGFONT lf_Font = {
			 -10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
			 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
			 DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE,
                         "Courier",
			 };
static LOGFONT lf_FontVar = {
  			 -10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
			 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
			 DEFAULT_QUALITY, 0, // FIXED_PITCH | FF_DONTCARE,
                         "MS Sans Serif",
			 };


    /* On a Japanese system, change the fonts...  should generalize to all FE */
    if (PRIMARYLANGID(GetUserDefaultLangID ()) == LANG_JAPANESE) {
        lf_Font.lfCharSet = SHIFTJIS_CHARSET;
        lstrcpy (lf_Font.lfFaceName , "Terminal");

        lf_FontVar.lfHeight= -12;
        lf_FontVar.lfCharSet = SHIFTJIS_CHARSET;
        lstrcpy (lf_Font.lfFaceName , "俵俽 僑僔僢僋");
    }



    switch (uMsg)
	{
	/* initialize menu before drawing */
	case WM_INITMENU:
	    InitMenu (hWnd);
	    break;

	/* display status messages for menu commands */
	case WM_MENUSELECT:
	    {
	    char     *lpszMenuString;

	    lpszMenuString = LocalAlloc (LPTR, MAX_PATH);

	    if (HIWORD (wParam) ==  0xFFFF)
		{
		LocalFree (lpszMenuString);
		lpszMenuString = NULL;
		}

	    else if (HIWORD (wParam) & MF_POPUP)
		{
		int	 i;
		HMENU	 hPopupMenu = GetSubMenu ((HMENU)lParam, LOWORD (wParam));

		lpszMenuString = LocalAlloc (LPTR, MAX_PATH);

		for (i=0; i<MENUPOPUPS; i++)
		    {
		    if (hPopup[i] == hPopupMenu)
			{
			LoadString (GetModuleHandle (NULL),
				    i+IDM_POPUPMENUS,
				    lpszMenuString,
				    MAX_PATH);
			break;
			}
		    }
		}
	    else
		LoadString (GetModuleHandle (NULL),
			    LOWORD (wParam),
			    lpszMenuString,
			    MAX_PATH);

	    /* send the status string, gray if necessary */
	    SendMessage (GetDlgItem (hWnd, IDC_STATUSWND),
			 WM_SETTEXT,
			 (HIWORD (wParam) & MF_GRAYED) ?
			     (LPARAM)GetSysColor (COLOR_GRAYTEXT):
			     0,
			 (WPARAM)lpszMenuString);

	    LocalFree (lpszMenuString);
	    }
	    break;

	case WM_CREATE:
	    {
	    HCURSOR	  hOldCursor;
	    HDC 	  hDC;
	    TEXTMETRIC	  tm;
	    char	  szWndClass[MAX_PATH];
	    HWND	  hList, hStatus;
	    int 	  i=0, j, k;
	    HMENU	  hMenu;

	    /* build array of popup menu handles */
	    hMenu = GetMenu (hWnd);
	    for (k=0; k<MENUPOPUPS-(i-1); k++)
		{
		hPopup[i] = GetSubMenu (hMenu, k);
		j=0;
		while ((hPopup[i+j+1] = GetSubMenu (hPopup[i], j)) != NULL)
		    j++;
		if (j)
		    i+=j;
		i++;
		}

	    /* put hourglass cursor up */
	    hOldCursor = (HCURSOR)SetClassLong (hWnd, GCL_HCURSOR, 0);
	    SetCursor (LoadCursor (0, IDC_WAIT));

	    hDC = GetDC(hWnd);

	    /* want a font with point size of 10 (smallest size it comes in) */
	    lf_Font.lfHeight = -(10 * GetDeviceCaps(hDC, LOGPIXELSY)/72);
	    hFont = CreateFontIndirect(&lf_Font);
	    hFontVar = CreateFontIndirect(&lf_FontVar);


            // find the width of a '0' in the variable font
            //
            SelectObject(hDC, hFontVar);
            GetTextExtentPoint (hDC, "0", 1, &sChar0);

            SelectObject(hDC, hFont);

	    /* initialize system constants */
	    GetTextMetrics(hDC, &tm);
	    yChar = tm.tmHeight;
	    xChar = tm.tmAveCharWidth;
	    xScreen = GetSystemMetrics(SM_CXSCREEN);
	    yScreen = GetSystemMetrics(SM_CYSCREEN);
	    yFrame = GetSystemMetrics(SM_CYFRAME);
	    xFrame = GetSystemMetrics(SM_CXFRAME);
	    yCaption = GetSystemMetrics(SM_CYCAPTION);
	    xVScrollBar = GetSystemMetrics(SM_CXVSCROLL);

	    //Actually, it's not good that a width of each column
	    // depends on a width of "0"!!
	    if (xChar > sChar0.cx) {
//		sChar0.cx = xChar;
		sChar0.cx = sChar0.cx * 12 / 10;	//sChar0.cx *= 1.2
	    }

            SelectObject(hDC, GetStockObject(SYSTEM_FONT));
            ReleaseDC(hWnd, hDC);

	    /* create listbox for client area */
	    LoadString (GetModuleHandle (NULL),
			IDS_LISTBOX,
			szWndClass,
			sizeof (szWndClass));
	    hList = CreateWindow (szWndClass,
				  NULL,
				  WS_CHILD | WS_VISIBLE | WS_VSCROLL |
				  LBS_EXTENDEDSEL | LBS_NOTIFY | LBS_OWNERDRAWFIXED |
				  LBS_WANTKEYBOARDINPUT | LBS_NOINTEGRALHEIGHT,
				  0, 0, 0, 0,
				  hWnd,
				  (HMENU)IDC_LISTBOX,
				  GetModuleHandle (NULL),
				  NULL);

	    /* if listbox failed, abort app */
	    if (!IsWindow (hList))
		DestroyWindow(hWnd);
	    SendMessage (hList, WM_SETFONT, (WPARAM)hFontVar, 0L);

	    /* create status window for client area */
	    LoadString (GetModuleHandle (NULL),
			IDS_STATUSCLASS,
			szWndClass,
			sizeof (szWndClass));
	    if (!(hStatus = CreateWindow (szWndClass,
					  NULL,
					  WS_CHILD | WS_VISIBLE | WS_BORDER,
					  0, 0, 0, 0,
					  hWnd,
					  (HMENU)IDC_STATUSWND,
					  GetModuleHandle (NULL),
					  NULL)))
		ReportError (IDS_ERRCREATEWINDOW);

	    /* initialize status window */
	    SendMessage (GetDlgItem (hWnd, IDC_STATUSWND),
			 UM_UPDATE,
			 (WPARAM)lpChildProcess,
			 0);

	    /* if child process post message to display initialization dialog */
	    if (lpChildProcess != NULL)
		PostMessage (hWnd, UM_STARTINITDIALOG, 0, 0);

	    /* remove hourglass cursor */
	    SetClassLong (hWnd, GCL_HCURSOR, (LONG)hOldCursor);
	    SetCursor (hOldCursor);
	    }
	    break;

	case UM_STARTINITDIALOG:
	    /* start modal initializing information window */
	    DialogBoxParam (GetModuleHandle (NULL),
			    (char *)IDD_INITIALIZING,
			    hWnd,
			    InitDlgProc,
			    (LPARAM)&hInitDlg);
	    hInitDlg = NULL;
	    break;

	case WM_SETFOCUS:
	    /* keep focus in listbox when possible */
	    SetFocus (GetDlgItem (hWnd, IDC_LISTBOX));
	    break;

	case WM_MEASUREITEM:
            ((MEASUREITEMSTRUCT FAR *)lParam)->itemHeight = sChar0.cy;
            break;

	case WM_DRAWITEM:
	    DrawListItem ((DRAWITEMSTRUCT FAR *)lParam);
            break;

	case WM_PAINT:
	    {
	    PAINTSTRUCT ps;
	    RECT rc;

	    /* draw the caption line above the list box */
	    BeginPaint(hWnd, &ps);
	    SetRect(&rc, 0, 0, GetSystemMetrics (SM_CXSCREEN), sChar0.cy);

	    SelectObject(ps.hdc, hFontVar);
	    SetTextColor(ps.hdc, GetSysColor(COLOR_CAPTIONTEXT));
	    SetBkColor(ps.hdc, GetSysColor(COLOR_ACTIVECAPTION));
            TextOutFields (ps.hdc, 6, &rc, szCaptionText);
	    SelectObject(ps.hdc, GetStockObject(SYSTEM_FONT));
	    EndPaint(hWnd, &ps);
	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -