locked
UAC program not Functioning properly. RRS feed

  • Question

  • hi MSDN,

    I am coding a program which will see if you are a Admin or not. The problem is that even though i am not a Admin it is say i am a Admin code :

    #include"Header.h" int main() { BOOL IsUserAdmin(VOID);

    // I got this of MSDN yet there is a Problem { BOOL b; SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; PSID AdministratorsGroup; b = AllocateAndInitializeSid( &NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup); if(b) { if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) { b = FALSE; MessageBox(NULL,"You are Not a Admin","Error",MB_OK); } else { FreeSid(AdministratorsGroup); MessageBox(NULL,"You are a Admin","Error",MB_OK); } } return(b); } }


    thanks Cyberwarfare


    computers make an world and program makes an empire in the vast world of computers and coder makes an emperor if master one language you can conquer the world of computers.


    • Edited by Sunder Hamu Sunday, December 2, 2012 6:20 PM
    • Moved by Damon Zheng Wednesday, December 19, 2012 11:23 AM forum retired (From:Visual C++ Language)
    Sunday, December 2, 2012 6:17 PM

Answers

  • I am coding a program which will see if you are a Admin or not. The problem is that even though i am not a Admin it is say i am a Admin code :

    #include"Header.h" int main() { BOOL IsUserAdmin(VOID);

    // I got this of MSDN yet there is a Problem { BOOL b; SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; PSID AdministratorsGroup; b = AllocateAndInitializeSid( &NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup); if(b) { if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) { b = FALSE; MessageBox(NULL,"You are Not a Admin","Error",MB_OK); } else { FreeSid(AdministratorsGroup); MessageBox(NULL,"You are a Admin","Error",MB_OK); } } return(b); } }


    There are some logic errors in your code because you
    took the MSDN example of a *function* and tried to
    insert it directly into the main function. You should
    have left it as a free-standing function and called it
    from main(). See if this works any better:

    #include "stdafx.h"
    #include <windows.h>
    
    BOOL IsUserAdmin(VOID)
    /*++ 
    Routine Description: This routine returns TRUE if the caller's
    process is a member of the Administrators local group. Caller is NOT
    expected to be impersonating anyone and is expected to be able to
    open its own process and process token. 
    Arguments: None. 
    Return Value: 
       TRUE - Caller has Administrators local group. 
       FALSE - Caller does not have Administrators local group. --
    */ 
    {
      BOOL b;
      SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
      PSID AdministratorsGroup; 
      b = AllocateAndInitializeSid(
        &NtAuthority,
        2,
        SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &AdministratorsGroup); 
      if(b) 
        {
        if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
          {
          b = FALSE;
          } 
      FreeSid(AdministratorsGroup); 
        }
    
      return(b);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      if(IsUserAdmin() == FALSE) 
        {
        MessageBox(NULL,_T("You are Not a Admin"),_T("Error"),MB_OK);
        }
      else
        {
        MessageBox(NULL,_T("You are a Admin"),_T("Error"),MB_OK);
        }
      return 0;
    }
    

    - Wayne

    • Marked as answer by Damon Zheng Wednesday, December 12, 2012 11:17 AM
    Monday, December 3, 2012 9:02 PM