嗨,我正试图从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: %un", 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: %un", 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 Openedn"));
}
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_MACHINEfoo
或HKEY_USERSfoo
,而不是HKEY_LOCAL_MACHINEfoobar
。此外,我认为您不能用已经存在的名称加载配置单元,例如,您不能将配置单元加载到HKEY_LOCAL_MACHINESOFTWARE
中。即使你能做到这些,你也会改变你对内容的看法,而不是合并它,当系统重新启动时,原始内容会重新出现。如前所述,如果要将数据插入现有配置单元,请使用RegRestoreKey
而不是RegLoadKey
。
您询问RegLoadKey
的用例:没有太多。大多数情况下,它是由操作系统使用的;例如,当你登录时,你的个人配置单元就是这样加载到HKEY_USERSusername
中的。有些奇怪的情况,比如离线重置密码或以其他方式修改另一个Windows实例的注册表。我在教学实验室的计算机上无人值守安装Windows的过程取决于修改Windows安装映像的注册表以禁用键盘和鼠标。