Document directory
- Predefined registry key
- CRegKey class and main usage and Function Description
Description
The Registry is an important part of Windows. It records a large amount of information about computer software and hardware. The value in the registry is identified by its name. The Value Name consists of characters with the same key name. The value itself can be a string, binary data, or 32-bit unsigned value. Here we mainly use the following skills:
Predefined registry key
The registry contains several predefined keys:
- HKEY_LOCAL_MACHINE contains entries describing the computer and its configuration. This includes information about the processor, system motherboard, memory, and installed software and hardware.
- HKEY_CLASSES_ROOT is the support key for document type and OLE \ COM-related information. This is the slave key of HKEY_LOCAL_MACHINE.
- HKEY_USERS is used as the default user's preferred setting and also as the preferred setting for a single user.
- HKEY_CLASSES_USER is the information used for the current (LOGIN) user.
- HKEY_CURRENT_CONFIG contains information about the current system configuration.
- There is another one, which generally does not appear, only after the configured LAN.
CRegKey class and main usage and Function Description
- Required header file: atlbase. h
- Common functions
☆Function for opening a key: RegOpenKeyEx Function Definition: LONG RegOpenKeyEx (HKEY hKey, // handle of the opened key, or it is directly the preceding root keys: LPCTSTR lpSubKey, // The address DWORD ulOptions of the sub-key name to be opened, // reserved value, must be 0 REGSAM samDesired, // open mode, for example, read or write a phkResult // return the opened sub-key handle); ☆query a key value: RegQueryValueEx Function Definition: LONG RegQueryValueEx (HKEY hKey, // The handle of the key to be queried, the name of the key value to be queried, LPDWORD lpReserved, // The reserved value, LPDWORD lpType, // The type of the data to be queried LPBYTE lpData, // The length of the data to be returned LPDWORD lpcbData // The length of the preset data); ☆set a key value RegSetValueEx Function Definition: LONG RegSetValueEx (HKEY hKey, // the handler of the key to be set, the name of the key value to be accessed, LPDWORD lpReserved, // reserved value, DWORD dwType, // The data type const BYTE * lpData to be set, // The length of the key value DWORD cbData // data );
Instance
- Create a dialog box-based program Regedit through AppWizard
- Set two command buttons: "query" (ID_QUERY) and "modify" (ID_CHANGE) to query and modify the user name and company name in the registry. Note: Information location 9x series: \ HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrntVersion NT series: \ HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrntVersion key value: registeredOwnertkg and RegisteredOrganization indicate the user name and company name respectively.
- Set two edit boxes to display and modify information. Define two CString variables m_strOwner and m_Company.
- The "query" button code is as follows:
Void cregeditdlg: onquery () {updatedata (true); hkey; // define the relevant hkey, close at the end of the query. // open the hkeylpctstr data_set = "SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \" related to the path data_set. // access the registry, hkey: Long ret0 = (: regopenkeyex (HKEY_LOCAL_MACHINE, data_set, 0, key_read, & hkey); If (ret0! = Error_success) // If the hkey cannot be opened, stop the program execution {afxmessagebox ("error: Unable to open the relevant hkey"); return ;} // query the related data lpbyte owner_get = new byte [80]; // define the user name owner_getdword type_1 = REG_SZ; // define the data type DWORD cbdata_1 = 80; // define the Data Length long ret1 =: regqueryvalueex (hkey, "registeredowner", null, & type_1, owner_get, & cbdata_1); If (ret1! = Error_success) {afxmessagebox ("error: Unable to query the relevant registry information"); Return ;}// query the company name lpbyte company_get = new byte [80]; // define the company name company_getdword type_2 = REG_SZ; // define the data type DWORD cbdata_2 = 80; // define the Data Length long ret2 =: regqueryvalueex (hkey, "registeredorganization", null, & type_2, company_get, & cbdata_2); If (ret2! = Error_success) {afxmessagebox ("error: Unable to query the relevant registry information"); Return ;}// display information m_strowner = cstring (owner_get); m_strcompany = cstring (company_get ); delete [] owner_get; Delete [] company_get; // when the program ends, close the enabled hkey: regclosekey (hkey); updatedata (false );}
- The "set" button code is as follows:
Void CRegeditDlg: OnModify () {UpdateData (true); HKEY hKEY; // defines the relevant hKEY, close at the end of the query. // open the hKEYLPCTSTR data_Set = "Software \ Microsoft \ Windows NT \ CurrentVersion \" related to the path data_Set. // access the registry, hKEY: long ret0 = (: RegOpenKeyEx (HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, & hKEY); if (ret0! = ERROR_SUCCESS) // If the hKEY cannot be opened, stop the program execution {AfxMessageBox ("error: Unable to open the relevant hKEY"); return ;} // set the relevant data // CString_To_LPBYTE. See the following function LPBYTE owner_Set = CString_To_LPBYTE (m_strOwner); // define the user name owner_SetDWORD type_1 = REG_SZ; // define the data type DWORD cbData_1 = m_strOwner.GetLength () + 1; // define the Data Length long ret1 =: RegSetValueEx (hKEY, "RegisteredOwner", NULL, type_1, owner_Set, cbData_1); if (ret1! = ERROR_SUCCESS) {AfxMessageBox ("error: Registry information cannot be set"); return ;}// query the company name LPBYTE company_Set = CString_To_LPBYTE (m_strCompany ); // define the company name company_SetDWORD type_2 = REG_SZ; // define the data type DWORD cbData_2 = m_strCompany.GetLength () + 1; // define the Data Length long ret2 =: RegSetValueEx (hKEY, "RegisteredOrganization", NULL, type_2, company_Set, cbData_2); if (ret2! = ERROR_SUCCESS) {AfxMessageBox ("error: unable to set the relevant registry information"); return ;}else {AfxMessageBox ("Registry Modification completed");} // The program ends, disable the enabled hKEY: RegCloseKey (hKEY); UpdateData (false );}
- Reference Functions
LPBYTE CString_To_LPBYTE(CString str){LPBYTE lpb=new BYTE[str.GetLength()+1];for(int i=0;i<str.GetLength();i++)lpb[i]=str[i];lpb[str.GetLength()]=0;return lpb;}
Notes
The above code can only be used in Windows NT/2000/XP. In Windows 9X/Me, change Windows NT in the Registry path to Windows. The source code of this program is tested in Windows 2000 + VC6.0. The sample file of the source code of this program can only be used in Windows NT/2000/XP. For Windows 9X/Me, modify the file and use it.