Registry processing-the registry for writing and deleting strap entries

Source: Internet
Author: User

Write the registry of the Entry

Bool addreginfostruct (preg_info_ctrl preginfoctrl) <br/>{< br/> bool success = true; <br/> preg_info_ctrl pregstructctrl = preginfoctrl; // create a new struct variable for storage, otherwise, the data in the original struct pointer is changed. <br/> If (pregstructctrl) <br/> {<br/> // name value next <br/> char name [260] = {0}; <br/> strcpy (name, pregstructctrl-> szkeyname); <br/> // create a key <br/> hkey; <br/> DWORD dw; //-1 indicates creation failed. 0 indicates creation successful. <br/> DW = regcreatekeyex (HKEY_CURRENT_USER, short2wide (n AME), 0, null, 0, 0, null, & hkey, & DW); // create or open the registry <br/> If (DW! = Error_success) <br/>{< br/> // MessageBox (null, _ T ("create false"), L "notice", mb_ OK ); <br/> const char * notice = "create false 11! "; <Br/> log (Notice, strlen (Notice); <br/> success = false; <br/> return success; <br/>}< br/> regclosekey (hkey); <br/> preg_value_ctrl pregvaluectrl = pregstructctrl-> pregvaluectrl; <br/> // write key value <br/> while (pregvaluectrl) <br/>{< br/> char valuename [260] = {0 }; <br/> strcpy (valuename, pregvaluectrl-> szvaluename); <br/> char value [260] = {0}; <br/> strcpy (value, pregvaluectrl-> szvalue); <br/> hkey; <br/> DWORD dw; <br/> regopenkeyex (HKEY_CURRENT_USER, short2wide (name), & hkey); // open the sub-key <br/> wchar_t * strkeyname = short2wide (valuename ); <br/> wchar_t * strkeyvalue = short2wide (value); <br/> long lresult; <br/> lresult = regsetvalueex (hkey, strkeyname, 0, REG_SZ, (byte *) strkeyvalue, wcslen (strkeyvalue) * 2); <br/> If (lresult! = 0) <br/>{< br/> const char * notice = "failed to set the value! "; <Br/> log (Notice, strlen (Notice); <br/> success = false; <br/> return success; <br/>}< br/> regclosekey (hkey); <br/> pregvaluectrl = pregvaluectrl-> pnext; <br/>}< br/> preg_info_ctrl * preginfoctrllist1 = new preg_info_ctrl [max_path]; <br/> zeromemory (preginfoctrllist1, (sizeof (preg_info_ctrl) * max_path ); <br/> Int J = 0; <br/> while (pregstructctrl-> psubkeyctrl [J]! = NULL) <br/>{< br/> preginfoctrllist1 [J] = pregstructctrl-> psubkeyctrl [J]; <br/> J ++; <br/>}< br/> for (INT I = 0; I <j; I ++) <br/> {<br/> preg_info_ctrl reginfoctrl = preginfoctrllist1 [I]; // retrieves each subitem, and each subitem is a complete struct, use recursion <br/> addreginfostruct (reginfoctrl); <br/>}< br/> If (null! = Preginfoctrllist1) <br/> Delete [] preginfoctrllist1; <br/>}< br/> return success; <br/>}< br/> 

Delete the registry of the Entry

On msdn

<Br/> msdn up and down, before I also wrote a </P> <p> http://msdn.microsoft.com/en-us/library/ms724235 (V = vs.85 ). aspx </P> <p> bool regdelnoderecurse (hkey hkeyroot, lptstr lpsubkey) <br/>{< br/> lptstr lpend; <br/> long lresult; <br/> DWORD dwsize; <br/> tchar szname [max_path]; <br/> hkey; <br/> filetime ftwrite; <br/> // first, see if we can delete the key without having <br/> // to recurse. <br/> lresult = regdeletekey (Hkeyroot, lpsubkey); <br/> If (lresult = error_success) <br/> return true; <br/> lresult = regopenkeyex (hkeyroot, lpsubkey, 0, key_read, & hkey); <br/> If (lresult! = Error_success) <br/>{< br/> If (lresult = error_file_not_found) {<br/> printf ("key not found. /n "); <br/> return true; <br/>}< br/> else {<br/> printf (" error Opening Key. /n "); <br/> return false; <br/>}< br/> // check for an ending slash and add one if it is missing. <br/> lpend = lpsubkey + lstrlen (lpsubkey); <br/> If (* (lpend-1 )! = Text ('//') <br/>{< br/> * lpend = text ('//'); <br/> lpend ++; <br/> * lpend = text ('/0'); <br/>}< br/> // enumerate the keys <br/> dwsize = max_path; <br/> lresult = regenumkeyex (hkey, 0, szname, & dwsize, null, <br/> null, null, & ftwrite ); <br/> If (lresult = error_success) <br/> {<br/> do {<br/> stringcchcopy (lpend, max_path * 2, szname ); <br/> If (! Regdelnoderecurse (hkeyroot, lpsubkey) {<br/> break; <br/>}< br/> dwsize = max_path; <br/> lresult = regenumkeyex (hkey, 0, szname, & dwsize, null, <br/> null, null, & ftwrite); <br/>} while (lresult = error_success ); <br/>}< br/> lpend --; <br/> * lpend = text ('/0'); <br/> regclosekey (hkey ); <br/> // try again to delete the key. <br/> lresult = regdeletekey (hkeyroot, lpsubkey); <br/> If (lresult = error_success) <br/> return true; <br/> return false; <br/>}< br/> 

Self-written

Bool deleteregistry (hkey hrootkey, lpctstr regpath) <br/>{< br/> DWORD dwerror = timeout; <br/> dworddwsubkeylength = max_key_length; <br/> lptstr psubkey = NULL; <br/> tcharsubregpath [max_key_length]; <br/> hkey hsubkey; <br/> If (regpath & lstrlen (regpath )) <br/> {<br/> If (regopenkeyex (hrootkey, regpath, 0, key_enumerate_sub_keys | Delete, & hsubkey) = error_success) <br/>{< br/> int I = 0; <br/> do {<br/> dwsubkeylength = max_key_length; <br/> If (regenumkeyex (hsubkey, I, subregpath, & dwsubkeylength, null) = error_no_more_items) <br/>{// delete a subregpath when no subitem exists <br/> regclosekey (hsubkey ); <br/> dwerror = regdeletekey (hrootkey, regpath); <br/> break; <br/>}< br/> else <br/> deleteregistry (hsubkey, subregpath ); // dwerror = <br/> I ++; <br/>}while (dwerror = error_success ); </P> <p >}< br/> else <br/> returnfalse; <br/> If (dwerror = error_success) <br/> returntrue; <br/> return false; <br/>} 

Note: when processing the registry key, remember to close it immediately after it is enabled. Similar to the serial port, you cannot operate on the registry key that has not been disabled.

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.