This question already has answers here:
How to use RegCopyTree
                                
                                    (3个答案)
                                
                        
                                3年前关闭。
            
                    
我正在尝试使用RegCopyTree将所有子项和值复制到另一个子项,但是我得到了错误代码5,这意味着ACCESS DENIED。

我要求访问KEY_CREATE_SUB_KEY,但是仍然失败。我还以管理员身份运行了该应用程序,但它根本无法正常工作。

你能帮助我吗?

这是我的代码:

int wmain()
{
    //RegOpenKeyEx
    HKEY hKey = HKEY_CURRENT_USER;
    LPCWSTR subKeyOpen = L"WinSide";
    DWORD options = 0;
    REGSAM samDesired = KEY_READ;
    HKEY openedKey;

    //Opening the HKCU\WinSide subkey
    LONG openKey = RegOpenKeyEx(hKey, subKeyOpen, options,
                                    samDesired, &openedKey);

    LPCWSTR subKeyDest = L"WinSi";
    REGSAM destSamDesired = KEY_CREATE_SUB_KEY;
    HKEY destOpenedKey;

    //Opening the HKCU\WinSi subkey - this is the destination subkey
    LONG destOpenKey = RegOpenKeyEx(hKey, subKeyDest, options,
                                destSamDesired, &destOpenedKey);


    if (openKey != ERROR_SUCCESS)
    {
        wprintf(L"Error opening the key. Code: %li\n", openKey);
    }
    else
    {
        wprintf(L"Key opened!\n");

        if (destOpenKey != ERROR_SUCCESS)
            wprintf(L"Error code: %li\n", destOpenKey);
        else
        {
            //RegCopyTree
            HKEY keyDest;

            LONG copyKey = RegCopyTree(openedKey, NULL, destOpenedKey);

            if (copyKey != ERROR_SUCCESS)
                wprintf(L"Error copying the key. Code: %li\n", copyKey);
            else
            {
                wprintf(L"Key copied!\n");
            }

        }



        RegCloseKey(openedKey);
        RegCloseKey(destOpenedKey);

    }

    return 0;
}

最佳答案

这是我解决问题的方法:

必须在REGSAM变量中使用KEY_ALL_ACCESS访问权限。这条路:

REGSAM samDesired = KEY_ALL_ACCESS;

REGSAM destSamDesired = KEY_ALL_ACCESS;

09-04 15:54