Write registry management programs with VC6 (3)

zhaozj2021-02-08  215

Write registry management programs with VC6 (3)

Solarsoft

Implementation of the list of CregexplorerView classes in the right: Define the private type variable hkey m_hKey; // communication function to pass the primary key cstring m_strsubkey; // subkey name 1. 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 (strTemp1.GetLength ()); 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, and the residual code 3 is the code 3. Operate the current value, double-click the name item, pop up the dialog, you need to implement the name of the double-click message nm_dblclk

void CRegExplorerView :: OnDblclk (NMHDR * pNMHDR, LRESULT * pResult) {OnRegModify (); * pResult = 0;} OnRegModify () is void with custom message menu CRegExplorerView :: OnRegModify () {long lResult; ASSERT (m_hKey ); if (GetListCtrl () GetSelectedCount ()> 1) {TRACE0 ( "CRegistryListCtrl :: OnRegModify -> Can not modify several values./n"); return;}. POSITION pos = GetListCtrl () GetFirstSelectedItemPosition (); int. Selecteditem = 0; selecteditem = getListCtrl (). GetnextSelectedItem (POS);

CString ItemType = getListCtrl (). GetItemText (SELECTEDITEM, 1);

IF (ItemType.Compare ("REG_SZ") == 0) // || ItemType.Compare ("No Type)") == 0) {CREGEDITSTRDLG EditstrDlg; cstract keyname, keydata; keyName = getListCtrl (). GetItemText (SelectedItem, 0); KeyData = getListCtrl (). GetItemText (SELECTEM, 2); editstrdlg.setstring (keyname, keydata); // if (itemtype.compare ("(not set)") == 0) // Keyname = ""

if (EditStrDlg.DoModal () == IDOK) {EditStrDlg.GetString (KeyName, KeyData); DWORD dwDataSize = KeyData.GetLength (); HKEY hKey; RegOpenKey (m_hKey, m_strSubKey, & hKey); RegSetValueEx (hKey, KeyName, 0, REG_SZ, (Byte *) Keydata.getBuffer (0), dwdatasize; regcloseKey (HKEY);}}

if (ItemType.Compare ( "REG_DWORD") == 0) {CRegEditDwordDlg EditDWORDDlg; CString KeyName = GetListCtrl () GetItemText (SelectedItem, 0);. DWORD KeyData; DWORD DataSize; HKEY hKey; DWORD dwType = REG_DWORD; DataSize = sizeof ( DataSize); lResult = RegOpenKeyEx (m_hKey, m_strSubKey, 0, KEY_QUERY_VALUE, & hKey);! if (lResult = ERROR_SUCCESS) return; lResult = RegQueryValueEx (hKey, KeyName, 0, & dwType, (BYTE *) & KeyData, & DataSize); if ( LRESULT! = Error_Success) Return;

EditDWORDDlg.SetStringDWORD (KeyName, KeyData); if (EditDWORDDlg.DoModal () == IDOK) {EditDWORDDlg.GetStringDWORD (KeyName, KeyData); RegSetValueEx (hKey, KeyName, 0, REG_DWORD, (BYTE *) & KeyData, DataSize);}

RegcloseKey (HKEY);

IF (itemtype.compare ("reg_binary") == 0) {cString Keyname = getListCtrl (). GetItemText (SELECTEDITEM, 0); CREGEDITBINDLG EditBinaryDLG;

HKEY hKey; BYTE Data [1024] = ""; DWORD dwDataSize = sizeof (Data); DWORD dwType = REG_BINARY; RegOpenKey (m_hKey, m_strSubKey, & hKey); RegQueryValueEx (hKey, KeyName, 0, & dwType, Data, & dwDataSize); EditBinaryDlg .SetStringData (Keyname, Data, DWDataSize);

}} And the display data; if (EditBinaryDlg.DoModal () == IDOK) {EditBinaryDlg.GetStringData (KeyName, Data, dwDataSize); RegSetValueEx (hKey, KeyName, 0, REG_BINARY, Data, dwDataSize);} RegCloseKey (hKey) The code for operation has been basically ended. Attachment: Engineering information To download it to http://solarsoft.126.com or above code program VC6, Win98 debugging.

Reference: Barretto VN of DriveExplorer Version 1.0 CodeProject's RegListCtrl WinApi32

转载请注明原文地址:https://www.9cbs.com/read-2537.html

New Post(0)