Recently, it is necessary to operate the Registry in a self-written program. I would like to sum up some experience and make a demo for future use. Now I will share it with you...
For ease of use, some operations are written 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. ");
Reference recommendations:
Perform simple operations on the registry
How to use the registry in VC ++