Perform simple operations on the registry

Source: Internet
Author: User
Preface

Recently, it is necessary to operate the Registry in a self-written program. Summarize some experiences and make a DEMO for future use. Now I will share it with you. I am a beginner at VC. Please give us some advice on any mistakes. For ease of use, I have written some operations as functions for convenient calling. The specific code is as follows.

Definition
HKEY hKey; char content [256]; // DWORD dwType = REG_SZ; // defines the data read type DWORD dwLength = 256; struct HKEY _ * RootKey; // registry primary key name TCHAR * SubKey; // the address of the registry key to be opened TCHAR * KeyName; // The name of the item to be set TCHAR * ValueName; // name of the value to be set: LPBYTE SetContent_S; // string type int SetContent_D [256]; // DWORD type BYTE SetContent_ B [256]; // binary int ShowContent (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName); int SetValue_S (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName, LPBYTE ReSetContent_S); int SetValue_D (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName, int ReSetContent_D [1, 256]); int SetValue_ B (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName, BYTE limit [256]); int DeleteKey (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReKeyName); int DeleteValue (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName );

View Functions
ShowContent (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName) {int I = 0; // Operation Result: 0 = succeedif (RegOpenKeyEx (ReRootKey, ReSubKey, 0, KEY_READ, & hKey) = ERROR_SUCCESS) {if (RegQueryValueEx (hKey, ReValueName, NULL, & dwType, (unsigned char *) content, & dwLength )! = ERROR_SUCCESS) {AfxMessageBox ("error: Unable to query the relevant registry information"); I = 1 ;}regclosekey (hKey);} else {AfxMessageBox ("error: unable to open the relevant hKEY "); I = 1;} return I ;}

Set string value functions
SetValue_S (struct HKEY _ * ReRootKey, TCHAR * ReSubKey, TCHAR * ReValueName, LPBYTE ReSetContent_S) {int I = 0; // Operation Result: 0 = succeed/int StrLength; // StrLength = CString (SetContent_S ). getLength (); if (RegOpenKeyEx (ReRootKey, ReSubKey, 0, KEY_WRITE, & hKey) = ERROR_SUCCESS) {if (RegSetValueEx (hKey, ReValueName, NULL, REG_SZ, ReSetContent_S, CString (SetContent_S ). getLength ())! = ERROR_SUCCESS) {AfxMessageBox ("error: Registry information cannot be set"); I = 1 ;}regclosekey (hKey);} else {AfxMessageBox ("error: unable to query the registry information "); I = 1;} return I ;}

DWORD Value Function
Setvalue_d (struct hkey _ * rerootkey, tchar * resubkey, tchar * revaluename, int resetcontent_d [256]) {int I = 0; // operation result: 0 = succeedif (regopenkeyex (rerootkey, resubkey, 0, key_write, & hkey) = error_success) {If (regsetvalueex (hkey, revaluename, null, REG_DWORD, (const unsigned char *) resetcontent_d, 4 )! = Error_success) {afxmessagebox ("error: Registry information cannot be set"); I = 1 ;}regclosekey (hkey);} else {afxmessagebox ("error: unable to query the registry information "); I = 1;} return I ;}

Set binary Functions
Setvalue_ B (struct hkey _ * rerootkey, tchar * resubkey, tchar * revaluename, byte resetcontent_ B [256]) {int I = 0; // operation result: 0 = succeedif (regopenkeyex (rerootkey, resubkey, 0, key_write, & hkey) = error_success) {If (regsetvalueex (hkey, revaluename, null, REG_BINARY, (const unsigned char *) resetcontent_ B, 4 )! = Error_success) {afxmessagebox ("error: Registry information cannot be set"); I = 1 ;}regclosekey (hkey);} else {afxmessagebox ("error: unable to query the registry information "); I = 1;} return I ;}

Delete subfunction
Deletekey (struct hkey _ * rerootkey, tchar * resubkey, tchar * rekeyname) {int I = 0; // Operation Result: 0 = succeedif (regopenkeyex (rerootkey, resubkey, 0, key_write, & hkey) = error_success) {If (regdeletekey (hkey, rekeyname ))! = Error_success) {// afxmessagebox ("An error occurred while clearing the specified item! "); I = 1;} regclosekey (hkey);} else {// afxmessagebox (" error: Unable to open the relevant hkey "); I = 1;} return I ;}

Delete a key-value function
Deletevalue (struct hkey _ * rerootkey, tchar * resubkey, tchar * revaluename) {int I = 0; // Operation Result: 0 = succeedif (regopenkeyex (rerootkey, resubkey, 0, key_write, & hkey) = error_success) {If (regdeletevalue (hkey, revaluename )! = Error_success) {// afxmessagebox ("failed to clear the specified value! "); I = 1;} regclosekey (hkey);} else {// afxmessagebox (" error: Unable to open the relevant hkey "); I = 1;} return I ;}

Call Method
Void CRegDemoDlg: OnSetValue_S () // code used in Example 1: Set the string value {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 1 "; // The name of the value to be set: SetContent_S = LPBYTE ("successful"); // The value content if (SetValue_S (RootKey, SubKey, ValueName, SetContent_S ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnSetContent_ B () // code used in Example 2: Set the binary value {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 2 "; // The name of the value to be set: SetContent_ B [0] = 1; // The value content // SetContent_ B [1] = 0x1B; // SetContent_ B [2] = 0x2C; // SetContent_ B [3] = 0x3D; // SetContent_ B [4] = 0x4E; if (SetValue_ B (RootKey, SubKey, ValueName, SetContent_ B ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnSetContent_D () // code used in Example 3: Set the DWORD Value {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 3 "; // The name of the value to be set: SetContent_D [0] = 4294967295; // The value content if (SetValue_D (RootKey, SubKey, ValueName, SetContent_D ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnDeleteValue_1 () // code used in Example 4 {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 1 "; // name of the value to be set if (DeleteValue (RootKey, SubKey, ValueName ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnDeleteValue_2 () // code used in Example 4 {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 2 "; // name of the value to be set if (DeleteValue (RootKey, SubKey, ValueName ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnDeleteValue_3 () // code used in Example 4 {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 3 "; // name of the value to be set if (DeleteValue (RootKey, SubKey, ValueName ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: OnDeleteKey () // code used in Example 5 {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft \ Windows \ CurrentVersion \ Explorer"; // The address KeyName = "Doc Find Spec MRU" to open the registry value "; // The name of the item to be set if (DeleteKey (RootKey, SubKey, KeyName ))! = 0) AfxMessageBox ("operation failed! ");} Void CRegDemoDlg: [view] In OnShowContent () // Example 1 {// TODO: Add your control notification handler code hereRootKey = HKEY_CURRENT_USER; // registry primary key name SubKey = "Software \ Microsoft"; // ValueName = "Example 1 "; // The name of the value to be set, if (ShowContent (RootKey, SubKey, ValueName) = 0) MessageBox (content, "this operation is completed using the ShowContent () function. ");}

This is just a few simple operations on the registry, I hope to help you.

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.