CAUTION: The check used below for testing whether a user is an Administrator is not 100% reliable, see the discussion in "User Account Control (UAC)" section of the link below and the references.
The code below is based on the C# solution found here (suggested in this comment by @Damien_The_Unbeliever)
Imports System.Runtime.InteropServices
Imports System.Security.Principal
<DllImport("advapi32.dll", SetLastError:=True)>
Private Shared Function GetTokenInformation(tokenHandle As IntPtr, tokenInformationClass As TokenInformationClass, tokenInformation As IntPtr, tokenInformationLength As Integer, ByRef returnLength As Integer) As Boolean
End Function
''' <summary>
''' Passed to <see cref="GetTokenInformation"/> to specify what
''' information about the token to return.
''' </summary>
Private Enum TokenInformationClass
TokenUser = 1
TokenGroups
TokenPrivileges
TokenOwner
TokenPrimaryGroup
TokenDefaultDacl
TokenSource
TokenType
TokenImpersonationLevel
TokenStatistics
TokenRestrictedSids
TokenSessionId
TokenGroupsAndPrivileges
TokenSessionReference
TokenSandBoxInert
TokenAuditPolicy
TokenOrigin
TokenElevationType
TokenLinkedToken
TokenElevation
TokenHasRestrictions
TokenAccessInformation
TokenVirtualizationAllowed
TokenVirtualizationEnabled
TokenIntegrityLevel
TokenUiAccess
TokenMandatoryPolicy
TokenLogonSid
MaxTokenInfoClass
End Enum
''' <summary>
''' The elevation type for a user token.
''' </summary>
Private Enum TokenElevationType
TokenElevationTypeDefault = 1
TokenElevationTypeFull
TokenElevationTypeLimited
End Enum
Private Function IsAdmin()
Dim identity = WindowsIdentity.GetCurrent()
If identity Is Nothing Then
Throw New InvalidOperationException("Couldn't get the current user identity")
End If
Dim principal = New WindowsPrincipal(identity)
' Check if this user has the Administrator role. If they do, return immediately.
' If UAC is on, and the process is not elevated, then this will actually return false.
If principal.IsInRole(WindowsBuiltInRole.Administrator) Then
Return True
End If
' If we're not running in Vista onwards, we don't have to worry about checking for UAC.
If Environment.OSVersion.Platform <> PlatformID.Win32NT OrElse Environment.OSVersion.Version.Major < 6 Then
' Operating system does not support UAC; skipping elevation check.
Return False
End If
Dim tokenInfLength As Integer = Marshal.SizeOf(GetType(Integer))
Dim tokenInformation As IntPtr = Marshal.AllocHGlobal(tokenInfLength)
Try
Dim token = identity.Token
Dim result = GetTokenInformation(token, TokenInformationClass.TokenElevationType, tokenInformation, tokenInfLength, tokenInfLength)
If Not result Then
Dim exception = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())
Throw New InvalidOperationException("Couldn't get token information", exception)
End If
Dim elevationType = DirectCast(Marshal.ReadInt32(tokenInformation), TokenElevationType)
Select Case elevationType
Case TokenElevationType.TokenElevationTypeDefault
' TokenElevationTypeDefault - User is not using a split token, so they cannot elevate.
Return False
Case TokenElevationType.TokenElevationTypeFull
' TokenElevationTypeFull - User has a split token, and the process is running elevated. Assuming they're an administrator.
Return True
Case TokenElevationType.TokenElevationTypeLimited
' TokenElevationTypeLimited - User has a split token, but the process is not running elevated. Assuming they're an administrator.
Return True
Case Else
' Unknown token elevation type.
Return False
End Select
Finally
If tokenInformation <> IntPtr.Zero Then
Marshal.FreeHGlobal(tokenInformation)
End If
End Try
End Function