Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Do I have administrator's privilegs ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Do I have administrator's privilegs ?

    Code:
       #Compile Exe
       #Dim All
       #Register None
       #Include "Win32Api.Inc"
    
       Function IsAdmin As Long
          Local os As OSVERSIONINFO
    
          os.dwOSVersionInfoSize = SizeOf(os)
          GetVersionEx ByVal VarPtr(os)
          If IsFalse(os.dwPlatformId = %VER_PLATFORM_WIN32_NT) Then Function = 2: Exit Function
    
          Local hAccessToken As Long, i As Long
          Local Info As String
          Local szInfo As Long
          Local pTokenGroups As TOKEN_GROUPS Ptr
          Local siaNtAuthority As SID_IDENTIFIER_AUTHORITY
          Local psidAdministrators As Long ' SID Ptr
    
          %TOKEN_QUERY = 8
          If IsFalse(OpenProcessToken(GetCurrentProcess, %TOKEN_QUERY, hAccessToken)) Then _
             Function = -1: Exit Function
          GetTokenInformation hAccessToken, ByVal %TOKENGROUPS, ByVal 0&, ByVal 0&, szInfo
          Info = Space$(szInfo): i = GetTokenInformation(hAccessToken, ByVal %TOKENGROUPS, ByVal StrPtr(Info), Len(Info), szInfo)
          CloseHandle hAccessToken
    
          If IsFalse(i) Then Function = -1: Exit Function
    
          siaNtAuthority.Value(5) = 5 ' = SECURITY_NT_AUTHORITY
          If IsFalse(AllocateAndInitializeSid(siaNtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, _
             %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators)) Then Function = -1: Exit Function
    
          pTokenGroups = StrPtr(Info)
          For i = 0 To @pTokenGroups.GroupCount - 1
             If EqualSid (ByVal psidAdministrators, ByVal @pTokenGroups.Groups(i).Sid) Then Function = 1: Exit For
          Next
          FreeSid ByVal psidAdministrators
    
       End Function
    
       Function PbMain
          Select Case IsAdmin
             Case  2: MsgBox "Under 9x"
             Case  1: MsgBox "Yes"
             Case  0: MsgBox "No"
             Case -1: MsgBox "Unexpected error"
          End Select
       End Function
    ------------------
    E-MAIL: [email protected]

  • #2
    To compile with the latest win32api.inc, change this line:
    If EqualSid (ByVal psidAdministrators, ByVal @pTokenGroups.Groups(i).Sid) Then Function = 1: Exit For
    to this:
    If EqualSid (ByVal psidAdministrators, ByVal @pTokenGroups.Groups(i).pSid) Then Function = 1: Exit For
    (just change .Sid to .pSid)

    ------------------
    -

    Comment

    Working...
    X