在C中使用RegCopyTree拒绝访问

时间:2016-08-26 17:34:10

标签: c winapi registry

我正在尝试使用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;
}

1 个答案:

答案 0 :(得分:0)

以下是我解决问题的方法:

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

REGSAM samDesired = KEY_ALL_ACCESS;

REGSAM destSamDesired = KEY_ALL_ACCESS;