Use C + + to manipulate the registry

Source: Internet
Author: User
Tags delete key function prototype numeric value reserved
RegCreateKeyEx through the RegCreateKeyEx function can be used in .... The RegSetValueEx function can set the value of a key in the registry.
Changing the registry now becomes another hot spot after overclocking, many cfan through the registry modification makes Win98 appear more personalized, many newspapers and magazines have to pull up the registration form this banner, unfortunately, in the introduction of the registry changes in many articles, mostly by manual modification as an example, A handful of articles only describe the use of modifying registry software, but no one mentions how to implement it in a program. For this purpose, I searched with the Registry keyword in the visual Studio6.0 Help file, with the help of the translation software and the continuous computer practice, I finally figured out some uses of the Registry interface (API) function, and now I'm sharing it with you.
Using the registry in a program is nothing more than creating, opening, reading, setting, and deleting these five general operations (for more detailed information, refer to the MSDN Library Visual studio6.0| in the Visual Studio6.0 Help catalog) Platform sdk| Window Base services| General library| Registry entries). Here we look at the use of these five interface functions:
First, create key RegCreateKeyEx
You can create a key in the registry by using the RegCreateKeyEx function, and if the key you want to create already exists, open the key. The function prototype is as follows:
LONG RegCreateKeyEx (
Hkey HKEY,
LPCTSTR Lpsubkey,
DWORD Reserved,
LPTSTR Lpclass,
DWORD dwoptions,
Regsam samdesired,
Lpsecurity_attributeslpsecurityattributes,
Phkey Phkresult,
Lpdword lpdwdisposition
);
The meanings of each parameter and return value are as follows:
? hkey the primary key value, you can take some of the following values:
HKEY_CLASSES_ROOT, Hkey_current_config
HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE
Hkey_user, Hkey_performance_data (Winnt operating system)
Hkey_dyn_data (Win9x operating system)
The parameter lpsubkey is a pointer to a 0-terminated string that contains the name of the subkey that will be created or opened. Subkeys may not start with a backslash (). The parameter can be null.
? Reserved reserved, must be set to 0.
? parameter Lpclass A string that points to the containing key type. If the key already exists, the parameter is ignored.
? parameter Dwoptions set a certain property for the newly created key. You can take some of the following values:
Reg_option_non_volatile the newly created key is a non transient key (data information is saved in the file and the data information is restored when the system restarts)
Reg_option_volatile the newly created key is a transient key (the data information is stored in memory). Windows95 ignores this value.
Reg_option_backup_restore is supported only in Winnt and can provide priority support.
The parameter samdesired is used to set permissions on key access, and you can take some of the following values:
Key_create_link allow symbol keys to be generated
Key_create_sub_key permission to generate subkeys
Key_enumerate_sub_keys allow generation of enum subkeys
Key_execute permission for read operations
Key_notify Permission to change notices
Key_query_value permission to query child keys
Key_all_access provides full access, a combination of the above values
Key_read is a combination of the following values:
Key_query_value, Key_enumerate_sub_keys, key_notify
Key_set_value allows you to set the number of subkeys
Key_write is a combination of the following values:
Key_set_value, Key_create_sub_key
The parameter lpsecurityattributes is a pointer to a security_attributes structure that determines whether the returned handle is inherited by the quilt processing process. If the argument is null, the handle cannot be inherited. In Winnt, this parameter can add a security description for the newly created key.
The parameter phkresult is a pointer to a handle to the newly created or opened key.
? parameter lpdwdispition Indicates whether the key was created or opened, and can be some of the following values:
The Reg_create_new_key key was not previously present and is now created.
The Reg_opened_existing_key key was previously present and is now open.
? The return value returns ERROR_SUCCESS if the function call succeeds. Otherwise, the return value is a non-zero error code defined in file WINERROR.h, and you can get a general description of the error by setting the Format_message_from_system identity to invoke the FormatMessage function.
Two, open a key RegOpenKeyEx
The RegOpenKeyEx function can open a specified key, and the function prototype is as follows:
LONG RegOpenKeyEx (
Hkey HKEY,
LPCTSTR Lpsubkey,
DWORD Uloption,
Regsam samdesired,
Phkey Phkresult
);
The meanings of each parameter and return value are as follows:
The meaning of the parameter hkey is the same as the HKEY parameter in the RegCreateKeyEx function.
The parameter lpsubkey is a pointer to a 0-terminated string that contains the name of the subkey, which can be separated by a backslash () with a different subkey name. If the string is empty, a new handle is created based on the HKEY parameter. In this case, the previously opened handle is not closed.
? uloption reserved, usually must be set to 0.
The meaning of the parameter samdesired is the same as the samdesired parameter in the RegCreateKeyEx function.
The parameter phkresult is a pointer to the handle of the open key. You can close this handle by using the RegCloseKey function.
? Returns the return value of the same value as the RegCreateKeyEx function.
Third, read key RegQueryValueEx
The RegQueryValueEx function allows you to read data from a key that is already open, and the function prototype is as follows:
LONG RegQueryValueEx (
Hkey HKEY,
LPTSTR Lpvaluename,
Lpdword lpreserved,
Lpdword Lptype,
Lpbyte lpdata,
Lpdword Lpcbdata
);
The meanings of each parameter and return value are as follows:
The parameter hkey is the handle to the current one open key, and the specific value is the HKEY parameter of the RegCreateKeyEx function.
The parameter lpvaulename to a non-empty string pointer to a name that contains the query value.
? lpreserved reserved, must be null.
The parameter lptype is a pointer to the data type, and the data type is one of the following types:
REG_BINARY binary Data
REG_DWORD 32-bit integer
Reg_dword_little_endian Little-endian format data, such as 0x12345678 saved in (0x78 0x56 0x34 0x12)
Reg_dword_big_endian Big-endian format data, such as 0x12345678 saved in (0x12 0x34 0x56 0x78)
REG_EXPAND_SZ a string containing an environment variable that is not extended
Reg_link a link to a Unicode type
REG_MULIT_SZ A string ending with two 0
Reg_none No type numeric value
Reg_resource_list Device driver Resource list
REG_SZ a 0-terminated string that is set to a Unicode or ANSI-type string based on the type of character set used by the function
The parameter lpdata is a pointer to a variable that holds the return value. If you do not need to return a value, this argument can be null.
The parameter lpcbdata is a pointer to a variable that holds the length of the returned value. Where the length is in bytes. If the data type is REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ, then the length also includes the ending 0 characters, and the parameter lpcbdata can be null only if the parameter is lpdata null.
? Returns the return value of the same value as the RegCreateKeyEx function.
Four, set the key value RegSetValueEx
The RegSetValueEx function can set the value of the key in the registry, and the function prototype is as follows:
LONG RegSetValueEx (
Hkey HKEY,
LPCTSTR Lpvaluename,
DWORD Reserved,
DWORD dwtype,
CONST BYTE *lpdata,
DWORD cbdata
);
The meanings of each parameter and return value are as follows:
The meaning of the parameter hkey is the same as the HKEY parameter in the RegCreateKeyEx function.
The parameter lpvaluename is a string pointer to the containing value name.
? Reserved is reserved, usually must be set to 0.
The parameter dwtype determines the type of the set value and the Lytype parameter of the Regqueryvaluekeyex.
The parameter lpdata to a pointer to a buffer that contains data.
The parameter cbdata specifies the length of the data in bytes.
? Returns the return value of the same value as the RegCreateKeyEx function.
Five, delete key value Regdeketekey
function Regdeketekey Deletes a key and all the subkeys. The function prototype is as follows:
LONG Regdeletekey (
Hkey HKEY,
LPCTSTR Lpsubkey
);
The meanings of each parameter and return value are as follows:
The meaning of the parameter hkey is the same as the HKEY parameter in the RegCreateKeyEx function.
The meaning of the parameter lpsubkey is the same as the Lpsubkey parameter in the RegCreateKeyEx function.
Vi. examples
Below we create a new project based on the dialog box in Visual c++6.0 or 5.0 environments. Set two command buttons called "Query user Information" and "Modify user Information" to query and modify the user name and company name in the registry. It should be explained that the user's information is located in the system registry at the location of the Key-current-usersoftwaremicrsoftms Setup (ACME) User info. Key-value names Defname and Defcompany represent the user's name and the name of the user's company, respectively.
1, query the user information code
HKEY hkey; Defines the hkey to be closed at the end of the query.
LPCTSTR path= "Software\micrsoft\ms Setup (ACME) \user info\";
LONG return0= (:: RegOpenKeyEx (Hkey_current_user,path,0,key_read,&hkey));
if (return0!=error_success)
{
MessageBox (Error: Unable to open the related key. ");
return;
}
Lpbyte username_get=new byte[80];
DWORD TYPE_1=REG_SZ;
DWORD cbdata_1=80;
LONG Return1=::regqueryvalueex (hkey, "Defname:,null,&type_1,
USERNAME_GET,&CBDATA_1);
if (return1!=error_success)
{
MessageBox ("Error: Unable to query for registry information.") ");
return;
}
Lpbyte company_get=new byte[80];
DWORD TYPE_2=REG_SZ;
DWORD cbdata_2=80;
LONG Return2=::regqueryvalueex (hkey, "Defcompany", null,&type_2,
company_get,&cbdata_2);
if (return2!=error_success)
{
MessageBox ("Error: Unable to query for registry information.") ");
return;
}
Converts Username_get and Company_get to CString strings to display output
CString str_username=cstring (Username_get);
CString str_company=cstring (Company_get);
Delete[] Username_get;
Delete[] Company_get;
Close the open hkey before the program ends
:: RegCloseKey (HKEY);
......
The string str_username and Str_company represent the name of the user who queried and the name of the company.
2, modify the user information code
Because the user outputs a string of type CString, you first convert it to a lpbyte type so that you can call the function later. Here is the conversion function:
Lpbyte cstring_to_lpbyte (CString str)
{
LPBYTE lpb=new BYTE [str. GetLength () +1];
for (int i=0;i<str. GetLength (); i++) lpb[i]= "Str[i";
Lpb[str. GetLength ()]=0;
return LPB;
}
The following is a specific code to modify the registry user information:
CString Str_username,str_company;
HKEY hkey;
LPCTSTR path= "Software\micrsoft\ms Setup (ACME) \user info\";
LONG Return0 (:: RegOpenKeyEx (Hkey_current_user,path,0,key_write,&hkey));
if (return0!=error_success)
{
MessageBox (Error: Unable to open the related key. ”);
Return
}
Lpbyte username_set=cstring_to_lpbyte (str_username);
DWORD TYPE_1=REG_SZ;
DWORD Cbdata_1=str_username. GetLength () +1;
LONG Return1=::regsetalueex (hkey, "defname?,null,type_1,username_set,cbdata_1");
if (return1!=error_success)
{
MessageBox (Error: Unable to modify information about the registry.) ”);
Return
}
Lpbyte company_set=cstring_to_lpbyte (Str_company);
DWORD TYPE_2=REG_SZ;
DWORD Cbdata_2=str_company. GetLength () +1;
LONG Return2=::regsetalueex (hkey, "Defcompany", null,type_2,company_set,cbdata_2);
if (return2!=error_success)
{
MessageBox (Error: Unable to modify information about the registry.) ”);
Return

}


Reproduced from: http://wenda.tianya.cn/wenda/thread?tid=665a94ba8ef76419

Related Article

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.