I've wrote a little routine which could be useful to some of you. It test if the currently logged is a member of the administrator group. It's also a good example of the NetAPI use for user and account management.
Code: Select all
; Need Windows NT/2000/XP
;
#SECURITY_BUILTIN_DOMAIN_RID = $00000020
Structure LOCALGROUP_MEMBERS_INFO_1
*lgrmi1_sid.l
lgrmi1_sidusage.l
*lgrmi1_name.l ; Warning, its WideCHAR
EndStructure
Procedure LookupAliasFromRid(TargetComputer, Rid, *Name)
sia.SID_IDENTIFIER_AUTHORITY
; #define SECURITY_NT_AUTHORITY {0,0,0,0,0,5} // ntifs
sia\Value[5] = 5
If AllocateAndInitializeSid_(@sia, 2, #SECURITY_BUILTIN_DOMAIN_RID, Rid, 0, 0, 0, 0, 0, 0, @*Sid)
NameSize = 1024
DomainNameSize = 1024
DomainName$ = Space(DomainNameSize)
Result = LookupAccountSid_(TargetComputer, *Sid, *Name, @NameSize, DomainName$, @DomainNameSize, @snu)
FreeSid_(*Sid) ;
EndIf
ProcedureReturn Result
EndProcedure
Procedure IsAdmininistrator()
; First, get the alias name for the admin group (depending of the langage)
;
AdministratorGroupName$ = Space(1024)
If LookupAliasFromRid(0, #DOMAIN_ALIAS_RID_ADMINS, @AdministratorGroupName$)
; Then, Get the current logged user name
;
LoggedUserNameSize = 1024
LoggedUserName$ = Space(LoggedUserNameSize)
If GetUserName_(@LoggedUserName$, @LoggedUserNameSize)
; Use the NetAPI32.dll. This is for Windows NT only (accounts support needed)
;
If OpenLibrary(0, "NetAPI32.dll" )
; Convert our admin group name into WideChar, as the API function need it
;
AdministratorGroupNameW$ = Space(1024)
MultiByteToWideChar_(#CP_ACP, #MB_PRECOMPOSED, AdministratorGroupName$, -1, AdministratorGroupNameW$, 1024)
; Enumerate all the members of the administrators group and see if the logged user is inside
;
If CallFunction(0, "NetLocalGroupGetMembers", 0, AdministratorGroupNameW$, 1, @*MembersBuffer.LOCALGROUP_MEMBERS_INFO_1, 10000, @NbEntriesRead, @NbTotalEntries, @ResumeHandle) = #ERROR_SUCCESS
For k=1 To NbEntriesRead
MemberName$ = Space(1024)
WideCharToMultiByte_(#CP_ACP, 0, *MembersBuffer\lgrmi1_name, -1, MemberName$, 1024, 0, 0)
If MemberName$ = LoggedUserName$
Result = 1
EndIf
; Go to the next user
;
*MembersBuffer+SizeOf(LOCALGROUP_MEMBERS_INFO_1)
Next
; Don't forget to release the buffer allocated by NetLocalGroupGetMembers()
;
CallFunction(0, "NetApiBufferFree", @*MembersBuffer)
EndIf
CloseLibrary(0)
EndIf
EndIf
EndIf
ProcedureReturn Result
EndProcedure
; The test code
;
Debug IsAdmininistrator()
Fred - AlphaSND