递归删除注册表项

时间:2009-05-18 14:31:10

标签: c++ windows-mobile registry

我需要在Windows Mobile 6下的Windows注册表中删除子树.RegDeleteTree函数不可用,并且SHDeleteKey(显然)在WM6 SDK下的任何静态库中都不可用,尽管声明在shlwapi中可用。小时。
我试图从shlwapi.dll获取它,比如

    typedef DWORD (__stdcall *SHDeleteKey_Proc) (HKEY, LPCWSTR);
    SHDeleteKey_Proc procSHDeleteKey; 
    HINSTANCE shlwapidll = ::LoadLibrary(_T("shlwapi.dll"));
    if(shlwapidll) {
    procSHDeleteKey = 
            (SHDeleteKey_Proc)GetProcAddress(shlwapidll,_T("SHDeleteKeyW"));
        ASSERT(procSHDeleteKey);
    }

但我点击了这个断言 是否有一种很好的方法可以在Windows Mobile下以递归方式删除注册表项(空或不)?

3 个答案:

答案 0 :(得分:6)

我想我在MSDN找到了答案。令我感到困惑的是,虽然通过SDK无法使用该功能 我也把MSDN中的代码放在这里,只是为了记录:

//*************************************************************
//
//  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.

    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 {

            StringCchCopy (lpEnd, MAX_PATH*2, 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.

    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[MAX_PATH*2];

    StringCchCopy (szDelKey, MAX_PATH*2, lpSubKey);
    return RegDelnodeRecurse(hKeyRoot, szDelKey);
}

答案 1 :(得分:0)

GetProcAddress的第二个参数是LPCSTR(也就是说,它不是 a LPCTSTR)。请删除_T()并尝试此操作:

GetProcAddress(shlwapidll, "SHDeleteKeyW");

这有助于解决问题吗?

答案 2 :(得分:-3)

您正在寻找RegDeleteTree()函数。只需用它替换RegDeleteKey。

显然,他们花了一段时间才弄明白,所以如果你想支持XP或更早版本,你需要自己实现它。