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

📄 monkey.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************\
*
*  PROGRAM: Monkey: the Registry Monkey Utility.
*  PURPOSE: To demonstrate Registry API.
*  COMMENTS:
*
\*************************************************************************/


#define STRICT
#include <windows.h>
#include <string.h>
#define _MBCS
#include <mbstring.h>
#include <stdlib.h>
#include <stdio.h>
#include "monkey.h"


HANDLE hInst;
HWND   hDlg;

HANDLE hHeap;


/*************************************************************************\
*
*  FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
*
*  PURPOSE: Creates the dialogbox.
*
*  COMMENTS:
*
\*************************************************************************/

int APIENTRY WinMain (HINSTANCE hInstance,
                      HINSTANCE hPrevInstance,
                      LPSTR     lpCmdLine,
                      int       nCmdShow)


{
  DWORD retCode;

  UNREFERENCED_PARAMETER( nCmdShow );
  UNREFERENCED_PARAMETER( lpCmdLine );
  UNREFERENCED_PARAMETER( hPrevInstance );

  hInst   = hInstance;
  hHeap   = HeapCreate (0, 0, 0);

  retCode = DialogBox ((HANDLE)hInst, (LPCSTR)"MonkeyDlg",
                        NULL, (DLGPROC)MonkeyDlgProc);

  HeapDestroy (hHeap);
  return  (retCode);

}

/************************************************************************\
*
*  FUNCTION: MonkeyDlgProc();
*
*  PURPOSE:  Handle the Monkey 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 MonkeyDlgProc (HWND hDlg, WORD wMsg, LONG wParam, LONG lParam)
{
   ULONG    KeyClassLength = 256;
   ULONG    KeyNameLength = 256;
   DWORD    indexLB;
   CHAR     *putNullAt;
   CHAR     lpBuffer1[128];
   CHAR     lpBuffer2[128];

   static   CHAR     RegPath[MAX_PATH]  = "";
   static   CHAR     NameLBSelect[256]  = "";
   static   HKEY     hKeyRoot;
   static   DWORD    RegLevel;
   static   BOOL     FullBranches = TRUE;

   static   HANDLE   hFile = INVALID_HANDLE_VALUE;
   static   HANDLE   hBootIni;

  UNREFERENCED_PARAMETER( lParam );

  switch (wMsg)
  {
      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);
        CheckDlgButton (hDlg, IDR_FULL, TRUE);
        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)"HKEY_LOCAL_MACHINE");

        SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
                     LB_ADDSTRING, 0, (LONG)"HKEY_CURRENT_USER");

        SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
                     LB_ADDSTRING, 0, (LONG)"HKEY_USERS");

        SendMessage (GetDlgItem(hDlg, IDL_LISTBOX),
                     LB_ADDSTRING, 0, (LONG)"HKEY_CLASSES_ROOT");

        hKeyRoot = 0;                   // Initialize hKeyRoot.
        return (0);

      case WM_SYSCOMMAND:
        if (wParam == SC_CLOSE)
        {
          EndDialog (hDlg, TRUE);
          if (hFile != INVALID_HANDLE_VALUE)
            CloseHandle (hFile);
          return (TRUE);
        }
        break;

      case WM_COMMAND:

        switch (LOWORD(wParam))
        {
            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 double click in left hand listbox, clear Value
               // edit fields, and execute Next functionality.
               if ( HIWORD (wParam) == LBN_DBLCLK)
               {
                 SetDlgItemText (hDlg, IDE_VALUE1, "");
                 SetDlgItemText (hDlg, IDE_VALUE2, "");
                 PostMessage (hDlg, WM_COMMAND, IDB_NEXT, 0);
               }
               return (0);

            case IDL_LISTBOX2:
                 // If double click right hand listbox, clear Value edit
                 // fields, then display the key's data.
               if ( HIWORD (wParam) == LBN_DBLCLK)
                {
                 SetDlgItemText (hDlg, IDE_VALUE1, "");
                 SetDlgItemText (hDlg, IDE_VALUE2, "");
                 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)
                 {
                 LoadString(hInst, IDS_SELECTMSG, lpBuffer1, sizeof(lpBuffer1));
                 LoadString(hInst, IDS_NAME, lpBuffer2, sizeof(lpBuffer2));
                 MessageBox (hDlg, lpBuffer1, lpBuffer2, 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);
                }


               // 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)
                 {
                 LoadString(hInst, IDS_TOPLEVEL, lpBuffer1, sizeof(lpBuffer1));
                 LoadString(hInst, IDS_NAME, lpBuffer2, sizeof(lpBuffer2));
                 MessageBox (hDlg, lpBuffer1, lpBuffer2, MB_OK);
                 return (0);
                 }

               //For all remaining cases, clear the listboxes.
               SendMessage (GetDlgItem (hDlg, IDL_LISTBOX),
                            LB_RESETCONTENT, 0, 0);
               SendMessage (GetDlgItem (hDlg, IDL_LISTBOX2),
                            LB_RESETCONTENT, 0, 0);



               // 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 (strcmp (RegPath, "") == 0)
                 {
                 SetDlgItemText (hDlg, IDE_NAME, "");
                 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 = _mbsrchr (RegPath, '\\')) == NULL)
                 {
                 RegPath[0] = '\0';

                 switch ((DWORD)hKeyRoot)
                   {
                   case (DWORD)HKEY_LOCAL_MACHINE:
                     strcpy (NameLBSelect, "HKEY_LOCAL_MACHINE");
                     break;

                   case (DWORD)HKEY_USERS:
                     strcpy (NameLBSelect, "HKEY_USERS");
                     break;

                   case (DWORD)HKEY_CURRENT_USER:
                     strcpy (NameLBSelect, "HKEY_CURRENT_USER");
                     break;

                   case (DWORD)HKEY_CLASSES_ROOT:
                     strcpy (NameLBSelect, "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 = '\0';
                 putNullAt = _mbsrchr (RegPath, '\\');

                 if (putNullAt)
                   {
                   strcpy (NameLBSelect, putNullAt+1);
                   *putNullAt = '\0';
                   }
                 else
                   {
                   strcpy (NameLBSelect, RegPath);
                   *RegPath = '\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;
    CHAR   Buf[100];
    CHAR   lpBuffer[128];


    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 (strcmp (RegPath, "") != 0)
        strcat (RegPath, "\\");

                                       // Add the next level child name.
      strcat (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) {
          LoadString(hInst, IDS_CANTOPENKEY, lpBuffer, sizeof(lpBuffer));
          wsprintf (Buf, lpBuffer);
        }
        else {
          LoadString(hInst, IDS_OPENKEYERR, lpBuffer, sizeof(lpBuffer));
          wsprintf (Buf, lpBuffer, retCode, __LINE__);
        }
        MessageBox (hDlg, Buf, "", MB_OK);
        PostMessage (hDlg, WM_COMMAND, IDB_BACK, 0);
        return;
        }

      }
    else
      {
      // Set the *hKeyRoot handle based
      // on the text taken from the ListBox.

      if (strcmp (NameLBSelect, "HKEY_CLASSES_ROOT") == 0)
        *hKeyRoot = HKEY_CLASSES_ROOT;

      if (strcmp (NameLBSelect, "HKEY_USERS") == 0)
        *hKeyRoot = HKEY_USERS;

      if (strcmp (NameLBSelect, "HKEY_LOCAL_MACHINE") == 0)
        *hKeyRoot = HKEY_LOCAL_MACHINE;

      if (strcmp (NameLBSelect, "HKEY_CURRENT_USER") == 0)

⌨️ 快捷键说明

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