Write registry management programs with VC6 (2)
Solarsoft 1. Use a tree to display the registry information. Note: The registry information is more, it is impossible to display all, then you can first display the closest to the tree root, and then gradually display the scheme of the subtree according to the operation, 1) Initialize the tree void CregEftView :: OnInitialupdate ) {CTreeView :: OnInitialUpdate (); / * define TreeCtrl icon * / m_pImageList = new CImageList (); CWinApp * pApp = AfxGetApp (); ASSERT (m_pImageList = NULL);! m_pImageList-> Create (16, 16, ILC_COLOR8 | ILC_MASK, 9, 9); m_pimagelist-> add (PAPP-> Loadicon); m_pimagelist-> add (PAPP-> Loadicon (ICO_OpenFLD)); m_pimagelist-> add (PAPP-> Loadicon (ICO_CLSDFLD)); GetTreeCtrl () SetImageList (m_pImageList, TVSIL_NORMAL);.. HTREEITEM hParent = GetTreeCtrl () InsertItem (MYCOMPUTER, ILI_MYCOMP, ILI_MYCOMP); // display roots, MYCOMPUTER macro is defined as "my computer" InitTreeView (hParent); // initialize primary key GetTreeCtrl () Expand (hParent, TVE_EXPAND);.} void CRegLeftView :: InitTreeView (HTREEITEM hParent) {HTREEITEM hItem; for (int i = 0; i <6; i ). {hItem = GetTreeCtrl () InsertItem (HKey_Root [ I], ILI_CLSDFLD, ILI_OPENFLD, HPAR ENT); AdddumMynode (HITEM); // Add Identification Tree}} 2) Send Tree View Message TVN_ItemExPanding and TVN_SelChanging Writing, the former is extension (or shrink), the latter changes the current selection.
Function body follows: void CRegLeftView :: OnItemexpanding (NMHDR * pNMHDR, LRESULT * pResult) {NM_TREEVIEW * pNMTreeView = (NM_TREEVIEW *) pNMHDR; HKEY hKey; HTREEITEM hItem = pNMTreeView-> itemNew.hItem; if (IsSubTree (hItem)!) {Cstring strdataname = getDataFromitem (hITEM); // Returns the project name of "My Computer", which is the key path hkey = gethkey (STRDATANAME); // Returns the primary key of the current project name CString strsubkey; int i = strDataName.find ("//"); if (i == - 1) {strsubkey = "";} else strsubkey = strdataname.mid (i 1); cwaitcursor wait; if (pnmtreeview-> action == TVE_EXPAND) {IF ( STRDATANAME! = myComputer) {deletechildren (HITEM); // Remove all sub-tree if (! EnumerateKey (HKEY, HITEM)) // enumerate all subtraces of the current project * PRESULT = true;}}}* PResult = 0;} void CRegLeftView :: OnSelchanging (NMHDR * pNMHDR, LRESULT * pResult) {NM_TREEVIEW * pNMTreeView = (NM_TREEVIEW *) pNMHDR; if (i = 0!) {HKEY hKey; HTREEITEM hItem = pNMTreeView-> itemNew. HITEM; CSTRING STRDATANAME = getDataFromitem (HITEM); hkey = gethkey; cstring strsubkey; int i = strdataname.find ("//"); if (i == - 1) {strsubkey = "";} else strsubkey = strDataName.mid (i 1); cwaitcursor wait; cregexplorerdoc * pdoc = getDocument (); pdoc->
m_RegExplorerView-> DeleteAllItems (); // Code Document Type Communication with, and may be operated RegExplorerView if (strDataName == MYCOMPUTER) return; pDoc-> m_RegExplorerView-> DoListView (hKey, strSubKey);} i ; * pResult = 0; } Implementation of other functions: CString CregleftView :: getDataFromitem (htreeItem HITEM) {CString Str; While (HITEM! = Null) {cstring string = getTreeCtrl (). GetItemText (HITEM); if (String.right (1)! = "//") &&! str.isempty ()) String = "//"; str = string str; hITEM = gettreeCtrl (). getParentItem (HITEM);} // 8 is my computer length, for Is to remove my computer 4 word IF (str.Left (8) == mycomputer && str.getLength ()> 8) Str = str.mid (9); return str;} HKEY CREGLEFTVIEW :: gethkey (LPCTSTR STRKEY ) {HKEY hKeyRootName; CString cstrKeyRootName = strKey; int i = cstrKeyRootName.Find ( "//"); if (i = - 1) cstrKeyRootName = cstrKeyRootName.Left (i);! if (cstrKeyRootName == _T ( "HKEY_CL ASSES_ROOT ")) hKeyRootName = HKEY_CLASSES_ROOT; else if (cstrKeyRootName == _T (" HKEY_CURRENT_USER ")) hKeyRootName = HKEY_CURRENT_USER; else if (cstrKeyRootName == _T (" HKEY_LOCAL_MACHINE ")) hKeyRootName = HKEY_LOCAL_MACHINE; else if (cstrKeyRootName == _T ( "HKEY_USERS")) HKEYROOTNAME = HKEY_USERS; Else IF (CSTRKEYROOTNAME ==_T ("HKEY_PERFORMANCE_DATA)) HKEYROOTNAME = HKEY_PERFORMANCE_DATA;
else if (cstrKeyRootName == _T ( "HKEY_CURRENT_CONFIG")) hKeyRootName = HKEY_CURRENT_CONFIG; else if (cstrKeyRootName == _T ( "HKEY_DYN_DATA")) hKeyRootName = HKEY_DYN_DATA; return hKeyRootName;} / * one Tree Algorithm: the registry tree enum * / UINT CRegLeftView :: EnumerateKey (HKEY hKey, LPCTSTR strKey, HTREEITEM hItem) {HTREEITEM hSubItem one currently in progress; TCHAR strCurString [100]; long lResult; DWORD dwCurIndex = 0; HKEY hCurKey; CString strCurKey; CString strSubKey = strKey; CString str = strKey; if (strKey == "") {AddDummyNode (hItem); return 0;} CWaitCursor wait; // CRegExplorerDoc * pDoc = GetDocument (); // pDoc-> m_ExplorerView-> DeleteAllItems (); // second portion, joined to the support lResult the ListView = RegOpenKeyEx (hKey, strKey, 0, KEY_ENUMERATE_SUB_KEYS, & hCurKey); if (! lResult = ERROR_SUCCESS) return 0; do {lResult = RegEnumKey (hCurKey, dwCurI NDEX, STRCURSTRING, SIZEOF (STRCURSTRING);
if ((lResult == ERROR_NO_MORE_ITEMS) || (lResult == ERROR_INVALID_HANDLE)) {break;} else {strCurKey.Format ( "% s", strCurString);. hSubItem = GetTreeCtrl () InsertItem (strCurKey, 2, 1, hItem , TVi_sort); if (! Str.isempty () &&! Str.isempty () {strsubkey = str "//" strcurstring;} else {strsubkey = strcurstring;}}}} Adddummynode (HSUBITEM);} dwcurindex ;}} while; regclosekey (hcurkey); Return dwcurindex;} void cregleftview :: adddummynode (htreeItem HITEM) {
. GetTreeCtrl () InsertItem ( "", 2, 1, hItem);} UINT CRegLeftView :: (HTREEITEM hItem) {UINT nCount = 0 DeleteChildren; HTREEITEM hChild = GetTreeCtrl () GetChildItem (hItem);.! While (hChild = NULL ) {HtreeItem hnextitem = getTreeCtrl (). GetnextsiblingItem (HCHILD); getTreeCtrl (). DeleteItem (hchild); hchild = hnextitem; ncount ;} return ncount;
} Implementation of the list of the list CREGEXPLORVIEW class: Define the private type variable hkey m_hKey; // communication function from the class. The code to communicate with the document class. 2. The display data in the current tree CRegLeftView :: OnSelchanging has a communication function in pDoc-> m_RegExplorerView-> DoListView (hKey, strSubKey); void CRegExplorerView :: DoListView (HKEY hKey, LPCTSTR strSubKey) {LV_ITEM lvitem; int iActualItem; CString strTemp1 = " (Default) "", strTemp2 = "(not set type)", strTemp3 = "(not set key value)"; m_hKey = HKEY; m_strsubkey = strsubkey; lvitem.mask = lvif_text | lvif_image; lvitem.iitem = 0; lvitem. ISUBITEM = 0; lvitem.psztext = strTemp1.getBuffer ()); lvitem.iimage = ici_str; IactualItem = getListCtrl (). InsertItem (& lVItem);
GetListCtrl (). SetItemText (IactualItem, 1, Strtemp2.getBuffer (0)); getListCtrl (). SetItemText (IactualItem, 2, Strtemp3.getBuffer (0)); ENUMERATEVALUES (HKEY, STRSUBKEY); // Enumerate the subkey All items displayed on the ListView} void CRegExplorerView :: EnumerateValues (HKEY hKey, LPCTSTR cstrKey) {static HKEY hLastKey = hKey; LONG lResult; DWORD dwIndex = 0; HKEY hCurKey = hKey; DWORD dwKeyType; DWORD dwKeyDataLength, dwKeyNameLen; LPBYTE pbbinKeyData = NULL; TCHAR * tcKeyName = NULL; TCHAR tcDataType [1024] = _T ( ""); lResult = RegOpenKeyEx (hCurKey, cstrKey, 0, KEY_QUERY_VALUE, & hKey); if (! lResult = ERROR_SUCCESS) return;
DWORD LNOOFVALUES = 0; DWORD LLONGESTKEYNAMELEN = 1; DWORD LLONGESTDATALEN = 1;
lResult = RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL, NULL, & lNoOfValues, & lLongestKeyNameLen, & lLongestDataLen, NULL, NULL); if (! lResult = ERROR_SUCCESS) return; hLastKey = hKey;
LLONGESTKEYNAMELEN ; llongeestDatalen ;
TCKEYNAME = New Tchar [llongestKeyNamelen]; pbbinkeydata = new byte [llongeestDatalen];
CString CstrfinalData, cstrtemp;
While (True) {MEMSET (PbbinkeyData, 0, LlongestDatalen); Memset (TCKEYNAME, 0, LLONGESTKEYNAMELEN); dwkeytype = dwKeyDatalength = dwKeyNamelen = 0;
DWKEYNAMELEN = llongestKeyNamelen; dwkeydatalength = llongeestdatalen;
lResult = RegEnumValue (hKey, dwIndex, tcKeyName, & dwKeyNameLen, NULL, & dwKeyType, pbbinKeyData, & dwKeyDataLength); if (lResult == ERROR_NO_MORE_ITEMS) break; AddRegistryItem (tcKeyName, dwKeyType, pbbinKeyData, dwKeyDataLength, dwIndex); // display data in ListView
dwIndex ;} RegCloseKey (hKey); delete tcKeyName; delete pbbinKeyData;} int CRegExplorerView :: AddRegistryItem (CString Name, DWORD Type, void * Data, DWORD DataSize, int Row) {if (Name.IsEmpty ()) {DeleteAllItems () ; Name = "(default)";} lvitem item = {0}; item.mask = lvif_text | lvif_image; item.cchtextmax = name.getLength (); item.iitem = row; item.psztext = name.getBuffer (0 INT iposition;
CString FormatData; switch (Type) {case REG_DWORD_BIG_ENDIAN: case REG_DWORD:.. {Item.iImage = 1; iPosition = GetListCtrl () InsertItem (& Item); GetListCtrl () SetItemText (iPosition, 1, "REG_DWORD"); DWORD * dwData = (DWORD *) DATA; if (data! = Null) {formatdata.format ("0x% x (% d)", * dwdata, * dwdata); getListCtrl (). SetItemText (IPosition, 2, FormatData.GetBuffer) 0));} Break;
Case reg_binary: {item.iimage = 1; iposition = getListCtrl (). INSERTITEM (& item); getListCtrl (). setItemText (IPosition, 1, "REG_BINARY"); if (data! = null) {formatdata = formatbinary ((Byte *) Data, DataSize); GetListCtrl () SetItemText (iPosition, 2, FormatData.GetBuffer (0));} break;} case REG_EXPAND_SZ:. case REG_SZ: case REG_LINK: case REG_MULTI_SZ: {Item.iImage = 0; iPosition = GetListCtrl (). INSERTITEM (& item); getListCtrl (). SetItemText (iPosition, 1, "REG_SZ"); if (data! = Null) {FormatData.format ("/"% s / ", (char *) DATA GetListct RL (). setItemText (iposition, 2, formatdata.getBuffer (0));} Break;} default: {item.mask = lvif_text; iposition = getListCtrl (). InsertItem (& item); getListCtrl (). setItemText (iposition, 1, "REG_NONE"); Break;}}
Return iposition;} The display to this data has been basically completed, the rest of the data operation