|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Network Services IsUserAnAdmin: Determine if the Current User is an Administrator |
||
Posted: | Sunday April 24, 2005 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows XP | |
OS restrictions: | Shell version 5 or later (typically Windows 2000, Windows XP, and later) | |
Author: | VBnet - Randy Birch | |
Related: |
InitializeSecurityContext: Authenticate User via the NT Challenge Process LookupAccountName: Verify a User's Account LookupAccountSid: Determine if the Current Process is Running Under Admin Account OpenUserBrowser: Add Selection of Users and Groups |
|
Prerequisites |
Windows 2000 or later utilizing Shell v5.0 or later. |
|
This
demo takes the straightforward one-API approach to determine if the
current user is a member of the administrator group. It really won't get
simpler than this! So what's the difference between this code and the code at LookupAccountSid: Determine if the Current Process is Running Under Admin Account? From what I can tell, this code determines if the current user is a member of the admin group, whereas the GetTokenInformation demo determines if the owner of the current process is an admin. The code only works on Windows 2000 or later. |
BAS Module Code |
None. |
|
Form Code |
To a form, add a single command button (Command1) and a label (Label1) with the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Declare Function IsUserAnAdmin Lib "shell32" () As Long Private Sub Form_Load() Command1.Caption = "IsUserAnAdmin" Label1.Caption = "" End Sub Private Sub Command1_Click() Select Case IsUserAnAdmin() Case 1: Label1.Caption = "YES" Case False: Label1.Caption = "NO" End Select End Sub |
Comments |
This tip submitted by Norm Cook and by Wim van der Veen. If you receive an error
similar to "Can't find DLL entry point" under Windows 2000, use an
aliased declare for IsUserAnAdmin to refer to the API by ordinal
number:
Private Declare Function IsUserAnAdmin Lib "shell32" Alias "#680" () As Long |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |