Access and modify the system registry in VC ++

zhaozj2021-02-08  196

(Guangdong Sheri Min Sun Qiang)

The registry of Windows95 / 98 contains system configurations of Windows95 / 98, hardware configurations of the PC, Win32 applications, and other settings information for users. The registry and INI file are different, it is a multi-level tree data structure with six branches (root bones), each branch is composed of many keys and key values, and each key represents a specific configuration item. .

In actual programming, we have encountered problems to access and modify the entire tree structure information of Windows95 / 98 registry in Visual C , such as querying and modifying information about user names and company names in the registry. By programming, we have implemented information about querying and modifying the system registry in Visual C . The specific programming method is illustrated in an example.

In the Visual C 6.0 or 5.0 environment, newly built a dialog-based project, 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 is important to point out that the user's information is located in the system registry / hkey_local_machine / software / microsoft / windows / currentversion / location, key value name Registeredowner and RegisteredorGanization respectively indicate the name of the user and the name of the user.

1. Query the code of user information

HKEY HKEY; / / Define the relevant HKEY, turn off at the end of the query.

LPCTSTR DATA_SET = "Software // Microsoft // Windows // CurrentVersion //";

// Open HKEY associated with the path DATA_SET, the first parameter is the root key name, the second parameter table.

/ / Indicates the position of the key to access, the third parameter must be 0, key_read represents the way inquiry.

// Access the registry, HKEY saves the handle of the key opened by this function.

Long Ret = (: regopenkeyex (HKEY_LOCAL_MACHINE, DATA_SET, 0, Key_Read, & HKey);

if (Ret0! = Error_Success) // If you can't open HKEY, the execution of the program is terminated.

{MessageBox ("Error: Unable to open the relevant HKEY!");

Return;}

/ / Query the relevant data (user name wer_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 key value name to be queried, TYPE_1 indicates the type of query data, and Owner_Get Saves.

// The data of the query, CBDATA_1 represents the predetermined data length.

Long Ret1 = :: RegQueryValueex (HKEY, "Registeredowner", NULL,

& Type_1, Owner_get, & CBData_1);

IF (Ret1! = Error_Success)

{

MessageBox ("Error: Unable to check the information!");

Return;}

/ / Inquiry 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 check the information!");

Return;

}

// convert Owner_get and Company_GET to a CString string to display the output.

CString str_owner = cstring (Owner_get);

CSTRING STR_COMPANY = CSTRING (Company_GET);

DELETE [] Owner_get; delete [] Company_Get;

The Open HKEY is turned off before //.

:: regcloseKey (HKEY);

......

Thus, the above program is executed, the string Str_owner and Str_Company indicate the name of the user and the name of the company, and it is displayed in the way in the VC .

2. Modify the code of user information (note and the above query code belong to different function)

In the program, let's first display a dialog box that requires the user to enter the new user name and company name and press the confirm key to obtain a string of the CSTRING type. It is necessary to convert it to the LPBYTE (ie Unsigned Char *) type data type so that the back function call. Below is a conversion function that converts the CSTRING type to lpbyte in the program:

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 is the specific modification of the code for the registry user information:

CString str_owner, str_company;

... // Enter new user information by dialog box, save to STR_OWNER and STR_Company

// Define the relevant HKEY, at the end of the program to close.

HKEY HKEY;

LPCTSTR DATA_SET = "Software // Microsoft // Windows // CurrentVersion";

// Open the HKEY associated with the path DATA_SET, key_write means that it is written.

Long Ret = (:: 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), 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 name to access, and Owner_SET represents the new key value, Type_1 and CBData_1 represent the new value.

// Data type and data length

Long Ret1 = :: RegSetValueex (HKEY, "Registeredowner", NULL,

TYPE_1, OWNER_SET, CBDATA_1);

IF (Ret1! = Error_Success)

{

MessageBox ("Error: Unable to modify the checkp table information!");

Return;

}

// Modify the relevant data (company name companies_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 the checkp table information!");

Return;

}

After performing the operation above modified the registry, open the registry to view the specific value, you can see that the relevant data has been successfully modified.

The above instance tells how to access the Windows 98/95 system registry in VC , which we can easily query and modify any location of the registry. The above procedures are debugged in Visual C 6.0 (Visual C 5.0 and the like), and the result is correct.

Excerpt from "Computer News"

转载请注明原文地址:https://www.9cbs.com/read-2915.html

New Post(0)