Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PureBasic Adventures - IsUserAnAdministrator()

Featured Replies

Posted

This is a repost from the "PureBasic Adventures" blog...

I needed some code in PureBasic to check if the logged in user and/or running process is a member of the Administrator group. There is IsUserAnAdmin function, it works and is easy to include in your code...

If IsUserAnAdmin_()
  Debug "Running as an Adminstrator"
Else
  Debug "Running as a Limited User"
EndIf

Unfortunately as Microsoft states on MSDN it's a wrapper on a short lifespan, support for it ended with Windows Vista but the function still works in Windows 8.1. Microsoft suggests using the CheckTokenMembership function with the SID identifier NtAthority which, requires a little bit more code to be backward and future proof. Fortunately Microsoft provides example C++ code on MSDN, porting it to PureBasic requires a bit more work, the code below is a translation of this code...

; ------------------------------------------------------------------
;
;   PureBasic IsUserAnAdministrator() function to check if the callers process 
;   is a member of the Administrators group. Code taken from Microsofts
;   example shown at CheckTokenMembership function.
;   
;   Return Value:
;
;   TRUE - Caller has Administrators local group. 
;   FALSE - Caller does not have Administrators local group.
;
;   http://msdn.microsoft.com/en-us/library/windows/desktop/aa376389%28v=vs.85%29.aspx
;   
;   See SID structures:
;
;   http://msdn.microsoft.com/en-us/library/cc980032.aspx
;   http://technet.microsoft.com/en-us/library/cc778824%28v=WS.10%29.aspx
;
;   By Teddy Rogers / PureBasic 5.24 LTS
;
; ------------------------------------------------------------------

Prototype.i CheckTokenMembership(TokenHandle, SidToCheck, IsMember)
Global CheckTokenMembership.CheckTokenMembership

Prototype.i AllocateAndInitializeSid(pIdentifierAuthority, nSubAuthorityCount, dwSubAuthority0, dwSubAuthority1, dwSubAuthority2, dwSubAuthority3, dwSubAuthority4, dwSubAuthority5, dwSubAuthority6, dwSubAuthority7, pSid)
Global AllocateAndInitializeSid.AllocateAndInitializeSid

Prototype.i FreeSid(pSid)
Global FreeSid.FreeSid

Procedure IsUserAnAdministrator()
  Protected IsMember, *AdministratorsGroup
  
  Structure NtAuthority
    NtAuthority.b[6]
  EndStructure
  
  Define SECURITY_NT_AUTHORITY.NtAuthority
  
  If OpenLibrary(advapi32, "advapi32.dll")
    CheckTokenMembership = GetFunction(advapi32, "CheckTokenMembership")
    
    If CheckTokenMembership
      AllocateAndInitializeSid = GetFunction(advapi32, "AllocateAndInitializeSid")
      
      If AllocateAndInitializeSid
        FreeSid = GetFunction(advapi32, "FreeSid")
        
        If FreeSid
          SECURITY_NT_AUTHORITY\NtAuthority[5]=5
          
          ; The AllocateAndInitializeSid function allocates and initializes a security identifier (SID) with up to eight subauthorities.
          
          If AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, #SECURITY_BUILTIN_DOMAIN_RID, #DOMAIN_ALIAS_RID_ADMINS, #Null, #Null, #Null, #Null, #Null, #Null, @*AdministratorsGroup)
            CheckTokenMembership(#Null, *AdministratorsGroup, @IsMember)
          EndIf
          
          FreeSid(*AdministratorsGroup)
        EndIf
      EndIf
    EndIf
    CloseLibrary(advapi32)
  EndIf
  
  ProcedureReturn IsMember
EndProcedure

Debug IsUserAnAdministrator()

Ted.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.