Use VC ++ to modify the Registry-let's take the first step (suitable for beginners)

Source: Internet
Author: User
Use VC ++ Modify registry ---- Let's take the first step (suitable for beginners)
Reposted from: www.csdn.net
Opening Remarks

Are you looking forward to writing a "system modification (maintenance) tool software" with your hands "? I think the answer should be yes! You may also know that many of these tools rely on the Windows registry. You can modify system parameters by modifying the registry. maybe, you still don't think it's marginal, good, don't worry, let me take this articleArticle, Tell you something you must know before you can actually modify the registry. I assume that the reader of this article is a beginner. I try to use the simplest pen to describe how to modify the registry so that you can understand how to do it immediately. But of course, you must have a certain understanding of the Windows operating system and have basic VC++Programming BASICS (if not, it is recommended that you stop it and stop looking at it-I don't want to waste your time ).

What is the registry?

 

What is the registry? Maybe it often appears in your verbal form, but now I want you to immediately tell us what it is, how it works, and how it works? Can you? If not, please read this part with patience; otherwise, skip it.

Windows registry includes Windows system configuration, PC hardware configuration, Win32 ApplicationProgramAnd 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. The most intuitive example is why different users in windows have their own personalized settings, such as different wallpaper and different desktops. This is achieved through the registry.

Several words you must understand:

Primary Key: (also called an item in NT) You can think of it as a layer in the entire registry structure, a bit similar to the directory tree structure in the resource manager. So its icons and folder icons are a bit imaginary.

String Value: a string of ASCII characters.

Binary value: this is nothing to say, 0101 is also!

The instance is coming soon!

How do you know which of the following functions must be implemented by modifying the registry? Oh, there are a lot of "list" registry books (in fact, manuals) on the market. Pick one as you like. I promise you can afford it.

In the actual programming process, you will encounter two problems: how to access (read) the Registry, and how to modify the registry, and the latter is often the most attractive. let's talk with instances!

Assume that you want to implement the programming task: Get the user name and Company Name of the local machine from the registry. Then, change them to the name you want to specify.

In Visual C++ 6Create a dialog box-based project in. 0 and set two command buttons named "inquire" and "modify" for query and modification respectively.

Note: The user information is located at \ HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ in the system registry. The key value names registeredowner and registeredorganization indicate the user name and company name respectively. You can definitely find them in the Registry manuals.

1 .Code 
Hkey; // Define the relevant hkey and disable it 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.
// The location of the key to be accessed. The third parameter must be 0. 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 enabled, terminate the program execution.
{MessageBox ( " Error: The related hkey cannot be opened! " );
Return ;}
// Query related data (User Name 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 ".
// The name of the key value to be queried. type_1 indicates the type of the queried data, which is saved by owner_get.
// 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 registry information! " );
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 registry information! " );
Return ;
}
// Convert owner_get and company_get to a 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 str_owner and str_company strings indicate the names of the queried users and the company names. ++ Can be displayed in the dialog box.
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. First, convert it to lpbyte (that is, 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.
// Define the relevant hkey and close it at the end of the program.
Hkey;
Lpctstr data_set = " Software \ Microsoft \ Windows \ CurrentVersion " ;

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.