A simple read/write registry instance

Source: Internet
Author: User
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:

  1. 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.
  2. HKEY_CLASSES_ROOT is the support key for document type and OLE \ COM-related information. This is the slave key of HKEY_LOCAL_MACHINE.
  3. HKEY_USERS is used as the default user's preferred setting and also as the preferred setting for a single user.
  4. HKEY_CLASSES_USER is the information used for the current (LOGIN) user.
  5. HKEY_CURRENT_CONFIG contains information about the current system configuration.
  6. There is another one, which generally does not appear, only after the configured LAN.
CRegKey class and main usage and Function Description
  1. Required header file: atlbase. h
  2. 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
  1. Create a dialog box-based program Regedit through AppWizard
  2. 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.
  3. Set two edit boxes to display and modify information. Define two CString variables m_strOwner and m_Company.
  4. 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 );}
  5. 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 );}
  6. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.