AdjustTokenPrivileges 错误 ERROR_NOT_ALL_ASSIGNED

uns*_*der 3 c++ windows privileges winapi visual-c++

请告知以下代码中指出的错误,为什么会发生这种情况?我是 C++ 新手。

我查看了 StackO 和 MSDN(例如链接),但它们对我没有帮助,因为我无法弄清楚我做错了什么。

HANDLE hToken;

if (!OpenProcessToken(GetCurrentProcess(), 
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
{
    return FALSE;
}

{
SetPrivilege(hToken,L"SeBackupPrivilege",1 );
Run Code Online (Sandbox Code Playgroud)
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;
DWORD cb=sizeof(TOKEN_PRIVILEGES);
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, 
       cb, 
       NULL, 
       NULL) )
{ 
      printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); 
      return FALSE; 
} 

if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) // This is True. Why??

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

    /*
    The token does not have one or more of the privileges specified in the NewState parameter. 
    The function may succeed with this error value even if no privileges were adjusted. 
    The PreviousState parameter indicates the privileges that were adjusted.
    */
} 

return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*own 6

您无法授予自己尚未拥有的特权。其他一些进程(具有更高的权限)必须将它们授予您。