Read and write a simple example of the registry

Author:    Updated:2008-3-26 12:18:29
I. Description:
     Windows registry is an important component of the registry records of a large amount of computer hardware and software information. The value of the registry through its name logo. Of the names and keys the same characters. Their value can be string, binary data or unsigned 32-bit value. Here, we mainly use the following techniques:

(1), pre-defined registry keys. Registry contains several pre-defined keys:

HKEY_LOCAL_MACHINE description includes computers and configuration entries. Including information on processor, the system motherboard, memory, and installed software and hardware information.
HKEY_CLASSES_ROOT with the document type and OLE \ COM related to the support of key information. This is the key HKEY_LOCAL_MACHINE subordinate bond.
HKEY_USERS used as the default user preferences, as a single user's preferences.
HKEY_CLASSES_USER is for the current (Login) the user's information.
HKEY_CURRENT_CONFIG contain the current system configuration information.
Also, normally does not arise only after a LAN configuration.
(2), CRegKey category and the main function of the use and
Required header files: atlbase.h
☆ open a commonly used function keys function: RegOpenKeyEx
    Function definition: LONG RegOpenKeyEx (HKEY hKey, / / has been the key to open the handle, or is directly above several key root
                                LPCTSTR lpSubKey, / / To open the names of key address
                                DWORD ulOptions, / / retention value, must be 0
                                REGSAM samDesired, / / open manner, such as read or write
                                PHKEY phkResult / / Back to the opening of sub-button handle
                               );

☆ for a certain keys: RegQueryValueEx
    Function definition: LONG RegQueryValueEx (HKEY hKey, / / For the bond handle
                                   LPCTSTR lpValueName, / / For the name of the keys
                                   LPDWORD lpReserved, / / retention value
                                   LPDWORD lpType, / / For the type of data
                                   LPBYTE lpData, / / to return to the query data
                                   LPDWORD lpcbData / / data length Preferences
                                  );

☆ set a keys RegSetValueEx
    Function definition: LONG RegSetValueEx (HKEY hKey, / / To set the bond handle
                                 LPCTSTR lpValueName, / / To access the name of the keys
                                 LPDWORD lpReserved, / / retention value
                                 DWORD dwType, / / To set the type of data
                                 Const BYTE * lpData, / / To set the value -
                                 DWORD cbData / / data length
                                );


Second, the examples:
Based on AppWizard created by the dialog box procedure Regedit
Set up two command buttons, entitled "Search" (ID_QUERY) and "modifications" (ID_CHANGE), the registry used to query and modify user name and company name. Note: Information position 9 x Series: \ HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrntVersion NT Series: \ HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrntVersion keys: RegisteredOwnertkg RegisteredOrganization respectively and expressed user name and company name
Establishment of two edit box, used to display and modify information. CString m_strOwner define two variables, m_Company.
"Search" button code as follows: void CRegeditDlg:: OnQuery ()
(
         UpdateData (true);
HKEY hKEY; / / hKEY relating to the definition, to close at the end of enquiries

/ / Open the path data_Set related hKEY
LPCTSTR data_Set = "Software \ \ Microsoft \ \ Windows NT \ \ CurrentVersion \ \";
/ / Visit to the registry, the preservation of this function hKEY open by the bond handle
Long ret0 = (:: RegOpenKeyEx (HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, & hKEY));
If (ret0! = ERROR_SUCCESS) / / If you can not open hKEY, suspension of the implementation of procedures
(
AfxMessageBox ( "error: unable to open the hKEY");
Return;
)

/ / For enquiries about the data
LPBYTE owner_Get new BYTE = [80]; / / user-defined names owner_Get
DWORD type_1 = REG_SZ; / / defined data types
DWORD cbData_1 = 80 / / data length definition

Long ret1 =:: RegQueryValueEx (hKEY, "RegisteredOwner", NULL, & type_1, owner_Get, & cbData_1);
If (ret1! = ERROR_SUCCESS)
(
AfxMessageBox ( "error: unable to enquire about the registration information");
Return;
)

/ / Enquiries Company
LPBYTE company_Get new BYTE = [80]; / / definition of the company name company_Get
DWORD type_2 = REG_SZ; / / defined data types
DWORD cbData_2 = 80 / / data length definition

Long ret2 =:: RegQueryValueEx (hKEY, "RegisteredOrganization", NULL, & type_2, company_Get, & cb --
Previous:VC++ mail volume derived Foxmail
Next:Use memory-mapped file to improve the performance you
User Reviews
Site Search
Related Articles
Recommended article
AD