RegSaveKey returns ERROR_PRIVILEGE_NOT_HELD

前端 未结 2 861
小鲜肉
小鲜肉 2021-01-19 01:57

I\'m trying to save the contents of a particular registry key to a file using the RegSaveKey() API:

HKEY key;
LRESULT result = RegOpenKeyEx(HKEY_LOCAL_MACHIN         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-19 02:29

    Note that SetPrivilege() of reuben's answer is user-defined, according to MSDN, the function body goes thus...

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

提交回复
热议问题