RegDeleteKey函数删除子项并包含其所有值 及递归删除子项例子。请注意,key名称不区分大小写。

来源:互联网 发布:吓人软件 编辑:程序博客网 时间:2024/05/16 17:28
RegDeleteKey


The RegDeleteKey function deletes a subkey and includes all its values. Note that key names are not case sensitive.
RegDeleteKey函数删除子项并包含其所有值。请注意,key名称不区分大小写。
64-bit Windows:  On WOW64, 32-bit applications view a registry tree that is separate from the registry tree that 64-bit applications view. To enable an application to delete an entry in the alternate registry view, use the RegDeleteKeyEx function.
64位Windows:在WOW64上,32位应用程序查看与64位应用程序查看的注册表树不同的注册表树。要使应用程序删除备用注册表视图中的条目,请使用RegDeleteKeyEx函数。


LONG RegDeleteKey(
  HKEY hKey,
  LPCTSTR lpSubKey
);


Parameters
hKey 
[in] Handle to an open key. The key must have been opened with the DELETE access right. For more information, see Registry Key Security and Access Rights.
处理一个开放的key。必须使用DELETE访问权限打开key。有关更多信息,请参阅注册表key安全和访问权限。
This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following Predefined Keys:
该句柄由RegCreateKeyEx或RegOpenKeyEx函数返回,或者可以是以下预定义键之一:


HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS


Windows Me/98/95:  This parameter can also be the following value: 此参数也可以是以下值:


HKEY_DYN_DATA


lpSubKey 
[in] Pointer to a null-terminated string that specifies the name of the key to be deleted. It must be a subkey of the key that hKey identifies, but it cannot have subkeys. This parameter cannot be NULL. 
Key names are not case sensitive.
指向指定要删除的key名称的以null结尾的字符串的指针。它必须是hKey标识的key的子项,但它不能有子项。此参数不能为NULL。
key名称不区分大小写。
For more information, see Registry Element Size Limits.
有关详细信息,请参阅注册表元素大小限制.
Return Values
If the function succeeds, the return value is ERROR_SUCCESS.
如果函数成功,则返回值为ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined in Winerror.h. To get a generic description of the error, you can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag.
如果函数失败,则返回值是在Winerror.h中定义的非零错误代码。要获得错误的通用描述,可以将FormatMessage函数与FORMAT_MESSAGE_FROM_SYSTEM标志一起使用。
Remarks备注
A deleted key is not removed until the last handle to it is closed.
删除的键在其最后一个句柄关闭之前不会被删除。


The subkey to be deleted must not have subkeys. To delete a key and all its subkeys, you need to enumerate recursively the subkeys and delete them individually. To delete keys recursively, use the SHDeleteKey function.


要删除的子项不能有子项。要删除一个键及其所有子项,您需要递归地枚举子项并单独删除它们。要递归删除键,请使用SHDeleteKey函数。


Windows Me/98/95:  The function also deletes all subkeys and values. To delete a key only if the key does not have subkeys or values, use the SHDeleteEmptyKey function.
Windows Me / 98/95:该功能还会删除所有子项和值。要删除一个键,只要该键不具有子键或值,请使用SHDeleteEmptyKey函数。


Example Code 
For an example that uses this function, see Deleting a Key with Subkeys.


Requirements
Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95. 
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server. 
Header Declared in Winreg.h; include Windows.h.
 
Library Link to Advapi32.lib.
 
DLL Requires Advapi32.dll.  

Unicode Implemented as RegDeleteKeyW (Unicode) and RegDeleteKeyA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.


Deleting a Key with Subkeys  删除键以及子键


The example in this topic uses the RegOpenKeyEx, RegEnumKeyEx, and RegDeleteKey functions to delete a registry key with subkeys.To test this example, create the following registry key by using Regedt32.exe, and then add a few values and subkeys:
本主题中的示例使用RegOpenKeyEx,RegEnumKeyEx和RegDeleteKey函数来删除具有子键的注册表项。要测试此示例,请使用Regedt32.exe创建以下注册表项,然后添加几个值和子项:
HKEY_CURRENT_USER\SOFTWARE\TESTDIR


After running the code, use the F5 key to refresh the registry data, and notice that the TestDir key is deleted.
运行代码后,使用F5键刷新注册表数据,并注意到TestDir键被删除。
#include <windows.h>
#include <stdio.h>


//*************************************************************
//
//  RegDelnodeRecurse()
//
//  Purpose:    Deletes a registry key and all it's subkeys / values.
//
//  Parameters: hKeyRoot    -   Root key
//              lpSubKey    -   SubKey to delete
//
//  Return:     TRUE if successful.
//              FALSE if an error occurs.
//
//*************************************************************


BOOL RegDelnodeRecurse (HKEY hKeyRoot, LPTSTR lpSubKey)
{
    LPTSTR lpEnd;
    LONG lResult;
    DWORD dwSize;
    TCHAR szName[MAX_PATH];
    HKEY hKey;
    FILETIME ftWrite;


    // First, see if we can delete the key without having to recurse.
//首先,看看我们不用递归是否可以删除key。
    lResult = RegDeleteKey(hKeyRoot, lpSubKey);


    if (lResult == ERROR_SUCCESS) 
        return TRUE;


    lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);


    if (lResult != ERROR_SUCCESS) 
    {
        if (lResult == ERROR_FILE_NOT_FOUND) {
            printf("Key not found.\n");
            return TRUE;
        } 
        else {
            printf("Error opening key.\n");
            return FALSE;
        }
    }


    // Check for an ending slash and add one if it is missing.
//检查一个结束的斜杠,如果缺少则添加一个。
    lpEnd = lpSubKey + lstrlen(lpSubKey);


    if (*(lpEnd - 1) != TEXT('\\')) 
    {
        *lpEnd =  TEXT('\\');
        lpEnd++;
        *lpEnd =  TEXT('\0');
    }


    // Enumerate the keys 枚举键


    dwSize = MAX_PATH;
    lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite);


    if (lResult == ERROR_SUCCESS) 
    {
        do {
            lstrcpy (lpEnd, szName);
            if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
                break;
            }
            dwSize = MAX_PATH;
            lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
                                   NULL, NULL, &ftWrite);
        } while (lResult == ERROR_SUCCESS);
    }


    lpEnd--;
    *lpEnd = TEXT('\0');
    RegCloseKey (hKey);


    // Try again to delete the key.//再次尝试删除key。
    lResult = RegDeleteKey(hKeyRoot, lpSubKey);
    if (lResult == ERROR_SUCCESS) 
        return TRUE;
    return FALSE;
}


//*************************************************************
//
//  RegDelnode()
//
//  Purpose:    Deletes a registry key and all it's subkeys / values.
//  目的:删除注册表项及其所有子项/值。
//  Parameters: hKeyRoot    -   Root key
//              lpSubKey    -   SubKey to delete
//
//  Return:     TRUE if successful.
//              FALSE if an error occurs.
//
//*************************************************************


BOOL RegDelnode (HKEY hKeyRoot, LPTSTR lpSubKey)
{
    TCHAR szDelKey[2 * MAX_PATH];


    lstrcpy (szDelKey, lpSubKey);
    return RegDelnodeRecurse(hKeyRoot, szDelKey);
}


void main()
{
   BOOL bSuccess;


   bSuccess = RegDelnode(HKEY_CURRENT_USER, "Software\\TestDir");


   if(bSuccess)
printf("Success!\n");
   else
printf("Failure.\n");
}

阅读全文
0 0