AdjustTokenPrivileges错误ERROR_NOT_ALL_ASSIGNED

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

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

HANDLE hToken; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { return FALSE; } { SetPrivilege(hToken,L"SeBackupPrivilege",1 ); 

 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; } 

您不能授予您自己尚未拥有的权限。 其他一些进程(具有更高权限)必须授予他们。