c++ - RegLoadKey 给出错误代码 5(拒绝访问)

标签 c++ c windows registry key

您好,我正在尝试从 HKLM\\SYSTEM\\CurrentControlSet\\Services\\Fax 加载 key ,但出现错误 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).

最佳答案

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

您可能想改用 RegRestoreKey

附加:

据我所知,配置单元只能加载到注册表根目录,即它们必须是 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 安装镜像的注册表以禁用键盘和鼠标。

关于c++ - RegLoadKey 给出错误代码 5(拒绝访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226546/

相关文章:

环境变量中的 Windows 控制台宽度

c - MPI_Reduce 和 MPI_MIN 如何工作?

c - 排序堆栈

.net - Windows 10 内置了哪个.Net 版本?

C++通过ref返回成员的方法和get-set方法之间的区别

c++ - glUniformMatrix4fv 导致 GL_INVALID_OPERATION

c++ - union 提取数据

C++ while 循环和 for 循环不起作用

c - 使用c中的指针初始化结构体的构造函数值

c++ - 在 Windows x64 上构建 xlnt 库