How to access and modify the Registry in VC ++

Source: Internet
Author: User

The Windows 95/98/Me registry contains Windows 95/98/Me system configuration, PC hardware configuration, Win32 application, and other user settings. The Registry is different from the INI file. It is a multi-level tree data structure with six branches (root keys). Each branch is composed of many keys and key values, each key represents a specific configuration item.
In actual programming, we encountered the problem of how to access and modify the entire tree structure information of the Windows 95/98/Me registry in Visual C ++, for example, you can query and modify information about the user name and company name in the registry. Through programming practices, we can query and modify information about the system registry in Visual C ++. The following example describes the specific programming method.
Create a dialog box-based project in Visual C ++ 6.0 or 5.0, and set two command buttons named "query user information" and "Modify user information ", used to query and modify the user name and company name in the registry. It must be noted that the user information is located in the HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersion location in the system registry. The key value names RegisteredOwner and RegisteredOrganization indicate the user name and the company name respectively.
1. Code for querying user information
HKEY hKEY; // define the related hKEY, which must be disabled at the end of the query.
LPCTSTR data_Set = "Software \ Microsoft \ Windows \ CurrentVersion \";
// Open the hKEY related to the path data_Set. The first parameter is the root key name and the second parameter table.
// Indicates the location of the key to be accessed. The third parameter must be 0, and KEY_READ indicates that the key is queried.
// Access the registry, and hKEY stores the handle of the key opened by this function.
Long ret0 = (: RegOpenKeyEx (HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, & hKEY ));
If (ret0! = ERROR_SUCCESS) // If the hKEY cannot be opened, terminate the execution of the program
{MessageBox ("error: Unable to open the relevant hKEY! ");
Return ;}
// Query related data (owner_Get ).
LPBYTE owner_Get = new BYTE [80];
DWORD type_1 = REG_SZ; DWORD cbData_1 = 80;
// HKEY is the handle of the key opened by the RegOpenKeyEx () function, "RegisteredOwner ".
// Indicates the name of the key value to be queried, type_1 indicates the type of the queried data, and owner_Get is saved.
// The queried data. cbData_1 indicates the pre-set data length.
Long ret1 =: RegQueryValueEx (hKEY, "RegisteredOwner", NULL,
& Type_1, owner_Get, & cbData_1 );
If (ret1! = ERROR_SUCCESS)
{
MessageBox ("error: Unable to query information about the registry! ");
Return;
}
// Query related data (Company Name: company_Get)
LPBYTE company_Get = new BYTE [80];
DWORD type_2 = REG_SZ; DWORD cbData_2 = 80;
Long ret2 =: RegQueryValueEx (hKEY, "RegisteredOrganization", NULL, & type_2, company_Get, & cbData_2 );
If (ret2! = ERROR_SUCCESS)
{
MessageBox ("error: Unable to query information about the registry! ");
Return;
}
// Convert owner_Get and company_Get to CString to display the output.
CString str_owner = CString (owner_Get );
CString str_company = CString (company_Get );
Delete [] owner_Get; delete [] company_Get;
// Close the opened hKEY before the program ends.
: RegCloseKey (hKEY );
......
In this way, the strings str_owner and str_company indicate the name of the queried user and the name of the company. In VC ++, a dialog box is displayed.
2. Modify user information code
Note that the query code is different from the preceding function body.
In the program, a dialog box is displayed, asking the user to enter a new user name and company name and press the OK key to obtain the CString-type string. You must first convert it to the LPBYTE (unsigned char *) data type for subsequent function calls. The following is the conversion function used in the program to convert the CString type to LPBYTE:
LPBYTE CString_To_LPBYTE (CString str)
{
LPBYTE lpb = new BYTE [str. GetLength () + 1];
For (int I = 0; ibr> lpb [str. GetLength ()] = 0;
Return lpb;
}
The following code modifies the registry user information:
CString str_owner, str_company;
...... // Enter new user information in the dialog box and save it to str_owner and str_company.

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.