RegLoadKey提供错误代码5(拒绝访问)

时间:2012-09-01 09:25:21

标签: c++ c windows registry key

您好我正在尝试从HKLM\\SYSTEM\\CurrentControlSet\\Services\\Fax加载密钥,但我收到错误5(拒绝访问)。我无法弄清楚我的代码有什么问题。

这是我的代码

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

BOOL SetPrivilege(
    HANDLE hToken,          // access token handle
    LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
    BOOL bEnablePrivilege   // to enable or disable privilege
) 
{
    TOKEN_PRIVILEGES tp;
    LUID luid;

    if ( !LookupPrivilegeValue( 
        NULL,            // lookup privilege on local system
        lpszPrivilege,   // privilege to lookup 
        &luid ) )        // receives LUID of privilege
{
    printf("LookupPrivilegeValue error: %u\n", GetLastError() ); 
    return FALSE; 
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
    tp.Privileges[0].Attributes = 0;

// Enable the privilege or disable all privileges.

if ( !AdjustTokenPrivileges(
       hToken, 
       FALSE, 
       &tp, 
       sizeof(TOKEN_PRIVILEGES), 
       (PTOKEN_PRIVILEGES) NULL, 
       (PDWORD) NULL) )
{ 
      printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); 
      return FALSE; 
} 

if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)

{
      printf("The token does not have the specified privilege. \n");
      return FALSE;
} 

return TRUE;
}
void _tmain(int argc, TCHAR *argv[])
{

HKEY   hKey;
LONG   lErrorCode;
HANDLE ProcessToken;
LPCWSTR subkey =  L"SYSTEM\\CurrentControlSet\\Services\\Fax";


if (OpenProcessToken(GetCurrentProcess(), 
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &ProcessToken)) 
{

    SetPrivilege(ProcessToken, SE_BACKUP_NAME, TRUE);
    SetPrivilege(ProcessToken, SE_RESTORE_NAME, TRUE);
    SetPrivilege(ProcessToken, SE_RESTORE_NAME, TRUE);

}


lErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey ,
                              0, KEY_ALL_ACCESS, &hKey);
if (lErrorCode != ERROR_SUCCESS)
   {
  _tprintf(TEXT("Error in RegOpenKeyEx (%d).\n"), lErrorCode);
  return;
   }
else
{
    _tprintf(TEXT("Key is successfully Opened\n"));
}

lErrorCode = RegSaveKey(hKey,L"c:\\load.reg",0);
if (lErrorCode != ERROR_SUCCESS)
   {
  _tprintf(TEXT("Error in RegSaveKey (%d).\n"), lErrorCode);
  return;
   }
else
{
    _tprintf(TEXT("Key is successfully Saved \n"));
}

lErrorCode = RegLoadKey(HKEY_LOCAL_MACHINE,subkey,L"c:\\load.reg");
if (lErrorCode != ERROR_SUCCESS)
   {
  _tprintf(TEXT("Error in RegLoadKey (%d).\n"), lErrorCode);
  return;
   }
else
{
    _tprintf(TEXT("Key is successfully loaded \n"));
}

lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
   {
  _tprintf(TEXT("Error in closing the key (%d).\n"), lErrorCode);
  return;
   }
else
{
    _tprintf(TEXT("Key is successfully closed \n"));
}
}

这是输出

Key is successfully Opened 
Key is successfully Saved 
Error in RegLoadKey (5).

1 个答案:

答案 0 :(得分:3)

RegLoadKey只能用于将新配置单元加载到注册表中。您不能使用它来覆盖现有配置单元的子项。

您可能希望改为使用RegRestoreKey

其他:

据我所知,hives只能在注册表根目录下加载,即它们必须是HKEY_LOCAL_MACHINE\fooHKEY_USERS\foo,而不是HKEY_LOCAL_MACHINE\foo\bar。此外,我认为您不能加载具有已存在名称的配置单元,例如,您无法将配置单元加载到HKEY_LOCAL_MACHINE\SOFTWARE。即使你可以做这些事情,你也会改变你对内容的看法,而不是合并它,当系统重新启动时,原始内容会重新出现。如前所述,如果要将数据插入现有配置单元,请使用RegRestoreKey而不是RegLoadKey

您询问RegLoadKey的用例:没有多少。大多数情况下,它被操作系统使用;例如,这是您登录时个人配置单元加载到HKEY_USERS\username的方式。有一些奇怪的情况,例如resetting a password offline或以其他方式修改另一个Windows实例的注册表。我在教学实验室的计算机上无人参与安装Windows的过程取决于修改Windows安装映像的注册表以禁用键盘和鼠标。