Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

VB6 - Elevated Privileges

$
0
0
Ever since Microsoft introduced UAC (User Access Control) with Vista, you sometimes need to know if your application is running with administrative privileges. A quick search on the Net revealed some confusion about the use of the GetTokenInformation call. Using this call with the TOKEN_ELEVATION_TYPE parameter will yield the correct result as long as UAC is enabled. But if UAC is disabled or the built-in Administrator account is being utilized, the wrong result is returned. Using TOKEN_ELEVATION, the correct result is returned regardless of the state of UAC. The small program below demonstrates the difference on a Vista machine with UAC disabled, as well as returning the various user directories.

All Users Directory: C:\ProgramData
Default Users Directory: C:\Users\Default
General User Directory: C:\Users
Current User Directory: C:\Users\couttsj
Using TOKEN_ELEVATION_TYPE,
couttsj is not using elevated privileges, or UAC is turned off!
Using TOKEN_ELEVATION,
couttsj is using elevated privileges!

J.A. Coutts
Code:

Option Explicit

Private Const TOKEN_QUERY = (&H8)
Private Const TOKEN_ELEVATION_TYPE As Long = 18
Private Const TOKEN_ELEVATION As Long = 20
Private Declare Function GetAllUsersProfileDirectory Lib "userenv.dll" Alias "GetAllUsersProfileDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
Private Declare Function GetDefaultUserProfileDirectory Lib "userenv.dll" Alias "GetDefaultUserProfileDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
Private Declare Function GetProfilesDirectory Lib "userenv.dll" Alias "GetProfilesDirectoryA" (ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
Private Declare Function GetUserProfileDirectory Lib "userenv.dll" Alias "GetUserProfileDirectoryA" (ByVal hToken As Long, ByVal lpProfileDir As String, lpcchSize As Long) As Boolean
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, ByRef TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long

Private Sub Form_Activate()
    Dim sBuffer          As String
    Dim lRetLen          As Long
    Dim CurrentUser      As String
    Dim hToken          As Long
    Dim tkElevation      As Long
    Dim N%
    'create a string buffer
    sBuffer = String(255, 0)
    'retrieve the all users profile directory
    If GetAllUsersProfileDirectory(sBuffer, 255) = 0 Then _
        Err.Raise Err.LastDllError, , "Could not retrieve the all users profile directory!"
    'show the result
    Print "All Users Directory: " & StripTerminator(sBuffer)
    sBuffer = String(255, 0)
    If GetDefaultUserProfileDirectory(sBuffer, 255) = 0 Then _
        Err.Raise Err.LastDllError, , "Could not retrieve the default users profile directory!"
    Print "Default Users Directory: " & StripTerminator(sBuffer)
    sBuffer = String(255, 0)
    If GetProfilesDirectory(sBuffer, 255) = 0 Then _
        Err.Raise Err.LastDllError, , "Could not retrieve general users profile directory!"
    Print "General User Directory: " & StripTerminator(sBuffer)
    sBuffer = String(255, 0)
    If OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) = 0 Then _
        Err.Raise Err.LastDllError, , "Could not retrieve current Process Handle!"
    If GetUserProfileDirectory(hToken, sBuffer, 255) = 0 Then _
        Err.Raise Err.LastDllError, , "Could not retrieve the current users profile directory!"
    CurrentUser = StripTerminator(sBuffer)
    Print "Current User Directory: " & StripTerminator(sBuffer)
    N% = InStrRev(CurrentUser, "\")
    If N% > 0 Then CurrentUser = Mid$(CurrentUser, N% + 1)
    If GetTokenInformation(hToken, TOKEN_ELEVATION_TYPE, tkElevation, 4, lRetLen) = 0 Then
        Err.Raise Err.LastDllError, , "Could not retrieve Token Information!"
    Else
        Print "Using TOKEN_ELEVATION_TYPE,"
        Select Case tkElevation
            Case 1
                Print CurrentUser & " is not using elevated privileges," _
                    & " or UAC is turned off!"
            Case 2
                Print CurrentUser & " is using elevated privileges!"
            Case 3
                Print CurrentUser & " is not using elevated privileges!"
        End Select
    End If
    If GetTokenInformation(hToken, TOKEN_ELEVATION, tkElevation, 4, lRetLen) = 0 Then
        Err.Raise Err.LastDllError, , "Could not retrieve Token Information!"
    Else
        Debug.Print tkElevation
        Print "Using TOKEN_ELEVATION,"
        Select Case tkElevation
            Case 0
                Print CurrentUser & " is not using elevated privileges!"
            Case Else
                Print CurrentUser & " is using elevated privileges!"
        End Select
    End If
End Sub


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>