本帖最后由 pananning 于 2024-7-25 04:29 编辑
通过此函数设置了权限
[C++] 纯文本查看 复制代码 BOOL Privilege::SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
{
DWORD result = GetLastError();
PrintErrorMessage(result,TEXT("LookupPrivilegeValue error: "));
return false;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
{
DWORD result = GetLastError();
PrintErrorMessage(result, TEXT("AdjustTokenPrivileges error: "));
return false;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
std::wcerr << L"The token does not have the specified privilege." << std::endl;
return false;
}
return true;
}
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &_hToken);
if (!SetPrivilege(_hToken, SE_BACKUP_NAME, TRUE) || !SetPrivilege(_hToken, SE_RESTORE_NAME, TRUE))
{
_tprintf(TEXT("SetPrivilege failed.\n"));
CloseHandle(_hToken);
}
其他路径的键值都可以读取,就HKEY_LOCAL_MACHINE\SECURITY不行
|