📄 regviewer.cpp
字号:
/******************************************************************************\
* This is a part of the Microsoft Source Code Samples.
* Copyright (C) 1993-1997 Microsoft Corporation.
* All rights reserved.
* This source code is only intended as a supplement to
* Microsoft Development Tools and/or WinHelp documentation.
* See these sources for detailed information regarding the
* Microsoft samples programs.
\******************************************************************************/
/*************************************************************************\
*
* PROGRAM: RegViewer: the Registry Viewer Utility.
* PURPOSE: To demonstrate Registry API.
* COMMENTS:
*
\*************************************************************************/
// PsPCMonkey.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "regviewer.h"
#include <commctrl.h>
#if _WIN32_WCE < 212 // Non Pocket PC devices
#error 'This sample works on Pocket PC devices only'
#endif
#include <aygshell.h>
HANDLE hInst;
HWND hDlg;
/*************************************************************************\
*
* FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
*
* PURPOSE: Creates the dialogbox.
*
* COMMENTS:
*
\*************************************************************************/
int APIENTRY WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow)
{
DWORD retCode;
UNREFERENCED_PARAMETER( nCmdShow );
UNREFERENCED_PARAMETER( lpCmdLine );
UNREFERENCED_PARAMETER( hPrevInstance );
hInst = hInstance;
retCode = DialogBox ((HINSTANCE)hInst, MAKEINTRESOURCE(IDD_VIEWERDLG),
NULL, (DLGPROC)ViewerDlgProc);
PostQuitMessage(0);
return (retCode);
}
/************************************************************************\
*
* FUNCTION: ViewerDlgProc();
*
* PURPOSE: Handle the Viewer dialog box messages.
*
* MESSAGES:
*
* WM_INITDIALOG - Posts WM_GETFIRSTKEY message.
*
* WM_GETFIRSTKEY - Puts the first 4 pre-defined keys in the listbox.
*
* IDL_LISTBOX - Trapped when an item in the left hand listbox
* has been double clicked. It posts a IDB_NEXT message.
*
* IDL_LISTBOX2 - Trapped when an item in the right hand listbox has
* been double clicked. It basically calls DisplayKeyData,
* which fills the Value edit fields with the data from
* the current key's specified value information.
*
* IDB_PRINT - Basically calls PrintTree() which does a recursive
* print of the Registry from the current key to the
* end of it's branches.
*
* IDB_BACK - Sets the dialog box with the information from the
* previously selected key (one closer to the root of
* the registry, the parent of the current key).
*
* IDB_NEXT - Sets the dialog box with the information on the
* selected key child.
*
* IDR_FULL - Sets a global variable used to determine if the
* user wants to print full Registry information
* or only information from keys that have value
* associated with it. Variable is set to TRUE.
*
* IDR_TRIMMED - Same as above, only the variable is set to FALSE.
*
\************************************************************************/
int APIENTRY ViewerDlgProc ( HWND hDlg,UINT uMsg, WPARAM wParam, LPARAM lParam )
{
ULONG KeyClassLength = 256;
ULONG KeyNameLength = 256;
DWORD indexLB;
TCHAR *putNullAt;
static TCHAR RegPath[MAX_PATH] = TEXT("");
static TCHAR NameLBSelect[256] = TEXT("");
static HKEY hKeyRoot;
static DWORD RegLevel;
static BOOL FullBranches = TRUE;
static HANDLE hBootIni;
UNREFERENCED_PARAMETER( lParam );
switch (uMsg)
{
case WM_INITDIALOG:
// Post a message to get the first 4 pre-defined keys, and set
// Full Branches to be the print default.
PostMessage (hDlg, WM_GETFIRSTKEY, 0, 0);
//Create the dialog full screen
SHINITDLGINFO shidi;
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
return (0);
case WM_GETFIRSTKEY:
// Initialize by putting the first 4 predefined keys of the
// registry in the list box.
SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG)TEXT("HKEY_LOCAL_MACHINE"));
SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG)TEXT("HKEY_CURRENT_USER"));
SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG)TEXT("HKEY_USERS"));
SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG)TEXT("HKEY_CLASSES_ROOT"));
hKeyRoot = 0; // Initialize hKeyRoot.
return (0);
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog (hDlg, TRUE);
return (TRUE);
case IDR_FULL:
// If Full Branches pressed, set global var to TRUE.
FullBranches = TRUE;
return (0);
case IDR_TRIMMED:
// If Trimmed Branches pressed, set global var to FALSE.
FullBranches = FALSE;
return (0);
case IDL_LISTBOX:
// If click listbox, clear Value
// edit fields, and execute Next functionality.
if ( LBN_SELCHANGE == HIWORD (wParam))
{
SetDlgItemText (hDlg, IDE_VALUE1, TEXT(""));
SetDlgItemText (hDlg, IDE_VALUE2, TEXT(""));
PostMessage (hDlg, WM_COMMAND, IDB_NEXT, 0);
}
return (0);
case IDL_LISTBOX2:
// If click listbox, clear Value edit
// fields, then display the key's data.
if ( LBN_SELCHANGE == HIWORD (wParam) )
{
SetDlgItemText (hDlg, IDE_VALUE1, TEXT(""));
SetDlgItemText (hDlg, IDE_VALUE2, TEXT(""));
DisplayKeyData (hDlg, RegPath, hKeyRoot);
}
return (0);
case IDB_NEXT:
// Get the index of the cursor selection
// in the list box.
indexLB = SendMessage (GetDlgItem (hDlg, IDL_LISTBOX),
LB_GETCURSEL, 0, 0);
// If nothing is selected, flag user and return, otherwise
// process the selected key.
// LB_ERR indicates nothing selected.
if (indexLB == LB_ERR)
{
MessageBox (hDlg, TEXT("Please select an item from the list box"),
TEXT("Registry Viewer Utility"), MB_OK);
return (0);
}
// If listbox item 0 is pressed, user wants to move
// back up. Execute the Back functionality.
if (indexLB == 0 && hKeyRoot)
{
PostMessage (hDlg, WM_COMMAND, IDB_BACK, 0);
return (0);
}
// clear previous contents first, if any.
SetDlgItemText (hDlg, IDE_VALUE1, TEXT(""));
SetDlgItemText (hDlg, IDE_VALUE2, TEXT(""));
SetDlgItemText (hDlg, IDE_TEXTOUT, TEXT(""));
// Get text from selection in LB.
SendMessage (GetDlgItem (hDlg, IDL_LISTBOX),
LB_GETTEXT, indexLB, (LPARAM)NameLBSelect);
// Put name of chosen item in Name field.
SetDlgItemText (hDlg, IDE_NAME, NameLBSelect);
// Then clear ListBox entries.
SendMessage (GetDlgItem (hDlg, IDL_LISTBOX),
LB_RESETCONTENT, 0, 0);
SendMessage (GetDlgItem (hDlg, IDL_LISTBOX2),
LB_RESETCONTENT, 0, 0);
EnumerateLevel (hDlg, NameLBSelect, RegPath, &hKeyRoot);
return (0);
case IDB_BACK:
// For this case (hRootKey = 0)you're at the top level already.
// Tell the user, then return
if (!hKeyRoot)
{
MessageBox (hDlg, TEXT("Top Level: You can not backup any further."),
TEXT("Registry Viewer Utility"), MB_OK);
return (0);
}
//For all remaining cases, clear the list and display boxes.
SendMessage (GetDlgItem (hDlg, IDL_LISTBOX),
LB_RESETCONTENT, 0, 0);
SendMessage (GetDlgItem (hDlg, IDL_LISTBOX2),
LB_RESETCONTENT, 0, 0);
SetDlgItemText (hDlg, IDE_VALUE1, TEXT(""));
SetDlgItemText (hDlg, IDE_VALUE2, TEXT(""));
SetDlgItemText (hDlg, IDE_TEXTOUT, TEXT(""));
// If hRootKey has a value, but the pathname is blank,
// then you must be 1 level deep, reset to level 0 by
// posting WM_GETFIRSTKEY.
if (lstrcmp (RegPath, TEXT("")) == 0)
{
SetDlgItemText (hDlg, IDE_NAME, TEXT(""));
PostMessage (hDlg, WM_GETFIRSTKEY, 0, 0);
return (0);
}
// Two cases left. One in which the path has only one
// key name in it, and no back slash character (meaning
// strrchr() will return NULL); and one the other case
// where there are more than one key name in the path (
// and at least one back slash for strrchr(). If this
// is the first case, we want to fakeout EnumerateLevel
// into thinking we just picked one of the pre-defined keys,
// and then re-enumerate it's child keys.
if ((putNullAt = _tcsrchr (RegPath, TEXT('\\'))) == NULL)
{
RegPath[0] = TEXT('\0');
switch ((DWORD)hKeyRoot)
{
case (DWORD)HKEY_LOCAL_MACHINE:
lstrcpy (NameLBSelect, TEXT("HKEY_LOCAL_MACHINE"));
break;
case (DWORD)HKEY_USERS:
lstrcpy (NameLBSelect, TEXT("HKEY_USERS"));
break;
case (DWORD)HKEY_CURRENT_USER:
lstrcpy (NameLBSelect, TEXT("HKEY_CURRENT_USER"));
break;
case (DWORD)HKEY_CLASSES_ROOT:
lstrcpy (NameLBSelect, TEXT("HKEY_CLASSES_ROOT"));
break;
}
SetDlgItemText (hDlg, IDE_NAME, NameLBSelect);
hKeyRoot = 0;
EnumerateLevel (hDlg, NameLBSelect, RegPath, &hKeyRoot);
}
else
{
// In the final case, we can just trim the last key
// name off the path, and re-enumerate the level.
*putNullAt = TEXT('\0');
putNullAt = _tcsrchr (RegPath, TEXT('\\'));
if (putNullAt)
{
lstrcpy (NameLBSelect, putNullAt+1);
*putNullAt = TEXT('\0');
}
else
{
lstrcpy (NameLBSelect, RegPath);
*RegPath = TEXT('\0');
}
SetDlgItemText (hDlg, IDE_NAME, NameLBSelect);
EnumerateLevel (hDlg, NameLBSelect, RegPath, &hKeyRoot);
}
return (0);
default:
return (0);
}
}
return (FALSE);
}
/************************************************************************\
*
* FUNCTION: EnumerateLevel();
*
* PURPOSE: To get a valid key handle (either to determine if the one sent
* to the function was one of the pre-defined, or to open a key
* specified by the path), and to pass that key handle along
* to QueryKey().
*
* To enumerate the children of a key, you must have
* an open handle to it. The four top keys of the
* Registry are predefined and open for use:
* HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_USER,
* and HKEY_CLASSES_ROOT. These 4 can be used for
* RegEnumKey as is; but to RegEnumKey on any of the
* children of these you must first have an open key
* handle to the child.
*
* If hKeyRoot != 0, assume you are lower than the
* first level of the Registry and the user is trying
* to enumerate one of the children. First calculate
* the name of the child, and then use RegOpenKey to
* get an open handle.
*
* If hKeyRoot == 0, assume you are at the top level
* of the Registry, and set the hKey to be enumerated
* to be one of the 4 predefined values, the specific
* one indicated by the ListBox selection.
*
\************************************************************************/
VOID EnumerateLevel (HWND hDlg, LPTSTR NameLBSelect,
LPTSTR RegPath, HKEY *hKeyRoot)
{
HKEY hKey;
DWORD retCode;
TCHAR Buf[80];
if (*hKeyRoot)
{
// If RegPath is not NULL, then
// you have to add a backslash to the
// path name before appending the next
// level child name.
if (lstrcmp (RegPath, TEXT("")) != 0)
lstrcat (RegPath, TEXT("\\"));
// Add the next level child name.
lstrcat (RegPath, NameLBSelect);
// Use RegOpenKeyEx() with the new
// Registry path to get an open handle
// to the child key you want to
// enumerate.
retCode = RegOpenKeyEx (*hKeyRoot,
RegPath,
0,
KEY_ENUMERATE_SUB_KEYS |
KEY_EXECUTE |
KEY_QUERY_VALUE,
&hKey);
if (retCode != ERROR_SUCCESS)
{
if (retCode == ERROR_ACCESS_DENIED)
wsprintf (Buf, TEXT("Error: unable to open key. Probably due to security reasons."));
else
wsprintf (Buf, TEXT("Error: Unable to open key, RegOpenKey = %d, Line = %d"),
retCode, __LINE__);
MessageBox (hDlg, Buf, TEXT(""), MB_OK);
PostMessage (hDlg, WM_COMMAND, IDB_BACK, 0);
return;
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -