Access and modify the system registry in VC ++

Source: Internet
Author: User

The Windows 95/98 registry contains Windows 95/98 system configuration, PC hardware configuration, Win32 application, and other user settings. The Registry is different from the INI file. It is a multi-layered tree data structure with six branch 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 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: "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. Note that the code for modifying user information is different from the preceding query code)

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 *) 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; I 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.
// Define the related hKEY and close it at the end of the program.
HKEY hKEY;
LPCTSTR data_Set = "Software \ Microsoft \ Windows \ CurrentVersion ″;
// Open the hKEY related to the path data_Set. KEY_WRITE indicates that the key is opened in write mode.
Long ret0 = (: RegOpenKeyEx (HKEY_LOCAL_MACHINE,
Data_Set, 0, KEY_WRITE, & hKEY ));
If (ret0! = ERROR_SUCCESS)
{
MessageBox ("error: Unable to open the relevant hKEY! ″);
Return;
}
// Modify the relevant data (User Name owner_Set). You must first convert the CString type to LPBYTE.
LPBYTE owner_Set = CString_To_LPBYTE (str_owner );
DWORD type_1 = REG_SZ;
DWORD cbData_1 = str_owner.GetLength () + 1;
// Similar to RegQureyValueEx (), hKEY indicates the handle of the opened key, "RegisteredOwner ″
// Indicates the key value to be accessed, owner_Set indicates the new key value, and type_1 and cbData_1 indicate the new value.
// Data type and length
Long ret1 =: RegSetValueEx (hKEY, "RegisteredOwner", NULL,
Type_1, owner_Set, cbData_1 );
If (ret1! = ERROR_SUCCESS)
{
MessageBox ("error: Unable to modify registry information! ″);
Return;
}
// Modify the relevant data (Company Name: company_Set)
LPBYTE company_Set = CString_To_LPBYTE (str_company );
DWORD type_2 = REG_SZ;
DWORD cbData_2 = str_company.GetLength () + 1;
Long ret2 =: RegSetValueEx (hKEY, "RegisteredOrganization", NULL,
Type_2, company_Set, cbData_2 );
If (ret2! = ERROR_SUCCESS)
{
MessageBox ("error: Unable to modify registry information! ″);
Return;
}

After modifying the registry, you can open the Registry to view the specific value. You can see that the data has been successfully modified.

The above example describes how to access the Windows 98/95 system registry in VC ++. We can easily query and modify information about any location of the Registry. The above programs are debugged in Visual C ++ 6.0 using Visual C ++ 5.0, and the running results are correct.


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.