2016-08-26 60 views
0

我想使用RegCopyTree將所有子項和值複製到另一個子項,但我得到錯誤代碼5,這意味着訪問被拒絕拒絕訪問拒絕RegCopyTree在C

我要求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; 
} 
+0

我在兩個開口都用'KEY_WRITE'嘗試過,但仍然出現同樣的錯誤。 –

+0

剛剛添加了答案。 –

回答

0

這是我如何解決這個問題:

只好用KEY_ALL_ACCESS訪問權限在REGSAM變量。這樣:

REGSAM samDesired = KEY_ALL_ACCESS; 

REGSAM destSamDesired = KEY_ALL_ACCESS;