Read and Write the Registry under VC

Source: Internet
Author: User

This article from http://www.vckbase.com/document/viewdoc? Id = 645

 

For ease of use, I have written some operations as functions for convenient calling. The specific code is as follows:

I. Definition

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 type
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[256]);int SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);int DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);int DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);

Ii. 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 ;}

3. 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 ;}

Iv. DWORD Value Functions

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 ;}

5. 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 ;}

6. Delete subitem Functions

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 ;}

7. 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 ;}

VIII. 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. ");

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.