问题描述
我正在尝试从HKLM\\SYSTEM\\CurrentControlSet\\Services\\Fax
加载密钥,但是出现错误5(访问被拒绝).我无法弄清楚我的代码出了什么问题.
Hi I'm trying to load a key from HKLM\\SYSTEM\\CurrentControlSet\\Services\\Fax
but i'm getting error 5 (Access Denied). I'm not able to figure it out what is wrong with my code.
这是我的代码
#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
仅可用于将新配置单元加载到注册表中.您不能使用它来覆盖现有配置单元的子项.
RegLoadKey
can only be used to load a new hive into the registry. You can't use it to overwrite a subkey of an existing hive.
您可能想改用RegRestoreKey
.
其他:
据我所知,配置单元只能在注册表根目录下加载,即它们必须是HKEY_LOCAL_MACHINE\foo
或HKEY_USERS\foo
,而不能是HKEY_LOCAL_MACHINE\foo\bar
.另外,我认为您不能使用已存在的名称加载配置单元,例如,您不能将配置单元加载到HKEY_LOCAL_MACHINE\SOFTWARE
中.即使您可以执行这些操作,也要更改内容视图,而不是合并视图,并且在重新启动系统后,原始内容会重新出现.如前所述,如果要将数据插入到现有配置单元中,请使用RegRestoreKey
而不是RegLoadKey
.
To the best of my knowledge, hives can only be loaded at a registry root, i.e., they must be HKEY_LOCAL_MACHINE\foo
or HKEY_USERS\foo
, never HKEY_LOCAL_MACHINE\foo\bar
. Also, I don't think you can load a hive with a name that already exists, e.g., you can't load a hive into HKEY_LOCAL_MACHINE\SOFTWARE
. Even if you could do these things, you'd be changing your view of the content, not merging it, and when the system was rebooted the original content would reappear. As previously mentioned, if you want to insert data into an existing hive, use RegRestoreKey
rather than RegLoadKey
.
您询问有关RegLoadKey
的用例:并不多.通常,它由操作系统使用.例如,这就是您登录时将个人配置单元加载到HKEY_USERS\username
中的方式.有一些奇怪的情况,例如脱机重设密码或以其他方式修改另一个Windows实例的注册表.我在教学实验室中的计算机上无人值守安装Windows的过程取决于修改Windows安装映像的注册表以禁用键盘和鼠标.
You ask about the use cases for RegLoadKey
: there aren't many. Mostly, it's used by the operating system; for example, that's how your personal hive is loaded into HKEY_USERS\username
when you log in. There are some oddball cases, such as resetting a password offline or otherwise modifying the registry of another Windows instance. The process I use for the unattended installation of Windows on the computers in my teaching lab depends on modifying the registry of the Windows installation image to disable the keyboard and mouse.
这篇关于RegLoadKey给出错误代码5(访问被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!