VC ++ recursively deletes the registry subkey

Source: Internet
Author: User

Vc6 does not provide a registry key with a sub-key to be deleted, while the latest. NET provides this function. However, many programs are still written in vc6.

I will send a self-developed code:

The test is absolutely usable!

/***
* Deletesubkeytree (hkey, lpctstr lpsubkey)
* Function: recursively delete a registry subkey.
* Entry:
* Hkey: A heky parameter, such as hkey_classes_root
* Lpsubkey: for example, ". EXE"
* Exit: The operation is successful. error_success is returned. If the operation fails, a non-0 value is returned.
*
* Instance: lpctstr lpsubkey = "shile ";
Hkey = hkey_classes_root;
Long ret;
Ret = deletesubkeytree (hkey, lpsubkey );
If (ret = error_success)
{
Setdlgitemtext (idc_static, "hkey_classes_root/shile deleted successfully ");
}
*
**************************************** **********/

Deletesubkeytree (hkey, lpctstr lpsubkey)
{

Long lresult;
Hkey hsubkey;
DWORD dwindex, cbname;
Char szsubkey [512];
Filetime ft;
Lresult = regopenkeyex (hkey, lpsubkey, 0, key_all_access, & hsubkey );
If (lresult! = Error_success)
{
Regclosekey (hsubkey );
Return lresult;
}
Dwindex = 0;
Cbname = sizeof (szsubkey)/sizeof (szsubkey [0]);
While (error_success = (lresult = regenumkeyex (hsubkey, dwindex, szsubkey, & cbname, null, null, & ft )))
{
Deletesubkeytree (hsubkey, szsubkey );
}
Regclosekey (hsubkey );
Lresult = regdeletekey (hkey, lpsubkey );
Return lresult;
}

Article Source: http://www.diybl.com/course/3_program/c++/cppxl/20081119/152041.html

This code is problematic and can be used as follows:

Link: http://blog.csdn.net/sding/archive/2008/12/14/3514562.aspx

//////////////////////////////////////// ///////////////////////////////////
// Deleregtree. cpp: defines the entry point for the console application.
// Author: sding
//////////////////////////////////////// ////////////////////////////////////

# Include "stdafx. H"
# Include <windows. h>
# Include <iostream>
Using namespace STD;

DWORD deletetree (hkey rootkey, const char * psubkey)
{

Hkey;
DWORD nret;
DWORD namecnt, namemaxlen;
DWORD keycnt, keymaxlen, maxdatelen;
Static char sformat [256] = "";
Strcat (sformat ,"----");

Static int dwdeep =-1;
Dwdeep ++;

Nret = regopenkeyex (rootkey, psubkey, 0, key_all_access, & hkey );
If (nret! = Error_success)
{
Cout <"can't open the Regedit ";
Return 0;
}

Nret = regqueryinfokey (hkey, null, & keycnt, & keymaxlen, null, & namecnt,
& Namemaxlen, & maxdatelen, null, null );
If (nret = error_success)
{
For (INT dwindex = keycnt-1; dwindex> = 0; dwindex --) // enumeration key value
{
Char skeyname [256] = "";
Regenumkey (hkey, dwindex, skeyname, sizeof (skeyname ));

Hkey hkeysub;
DWORD keycntsub;
Char psubkeytemp [256] = "";
Strcpy (psubkeytemp, psubkey );
Strcat (psubkeytemp ,"//");
Strcat (psubkeytemp, skeyname );

Nret = regopenkeyex (rootkey, psubkeytemp, 0, key_all_access, & hkeysub );
If (nret = error_success)
{
Nret = regqueryinfokey (hkeysub, null, & keycntsub, & keymaxlen, null, & namecnt,
& Namemaxlen, & maxdatelen, null, null );
If (nret = error_success)
{
If (keycntsub! = 0)
{
Deletetree (rootkey, psubkeytemp );
}
Regclosekey (hkeysub );
}
}

// Cout <sformat <skeyname <Endl;

Regdeletekey (rootkey, psubkeytemp );
}

Regclosekey (hkey );
}

// Sformat [strlen (sformat)-4] = 0;

If (dwdeep = 0)
{
Regdeletekey (rootkey, psubkey );
}

Return 0;
}

Int main (INT argc, char * argv [])
{
Deletetree (HKEY_LOCAL_MACHINE, "system // CurrentControlSet // control // safeboot // minimal ");

System ("pause ");
Return 0;
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/woyaowenzi/archive/2009/02/03/3861049.aspx

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.