|
-
Oct 19th, 2000, 04:16 AM
#1
Thread Starter
Addicted Member
hi there all,
I succeed to capture the current user on WinNT,
How do I find out to which Groups the current user belong to ?
tnx
LirLir
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 19th, 2000, 07:06 AM
#2
Lively Member
Put this into a module's declarations:
Code:
Public Const NERR_Success As Long = 0&
Public Declare Function NetUserGetGroups Lib "netapi32" (lpServer As Any, UserName As Byte, ByVal Level As Long, lpBuffer As Long, ByVal PrefMaxLen As Long, lpEntriesRead As Long, lpTotalEntries As Long) As Long
Public Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Public Declare Function lstrlenW Lib "KERNEL32" (ByVal lpString As Long) As Long
Declare Function NetApiBufferFree Lib "netapi32" (ByVal psBuffer As Long) As Long
and create the following functions:
Code:
Public Function GetGlobalGroups(ByVal psUsername_IN As String, ByRef psaGroups_OUT() As String) As Boolean
Dim lpBuffer As Long
Dim yUserName() As Byte
Dim yServer() As Byte
Dim lpGroups() As Long
Dim nRead As Long
Dim nTotal As Long
Dim nRet As Long
Dim i As Long
On Error GoTo Err_GetGlobalGroups
yUserName = psUsername_IN & vbNullChar
If gsServerName = "" Then
nRet = NetUserGetGroups(ByVal 0&, yUserName(0), 0, lpBuffer, &H4000, nRead, nTotal)
Else
If InStr(gsServerName, "\\") = 1 Then
yServer = gsServerName & vbNullChar
Else
yServer = "\\" & gsServerName & vbNullChar
End If
' Debug.Print "INFO: Retrieving User groups from " & CStr(yServer)
nRet = NetUserGetGroups(yServer(0), yUserName(0), 0, lpBuffer, &H400, nRead, nTotal)
End If
If nRet = NERR_Success Then
ReDim lpGroups(0 To nRead - 1) As Long
ReDim psaGroups_OUT(0 To nRead - 1) As String
CopyMem lpGroups(0), ByVal lpBuffer, nRead * 4
For i = 0 To nRead - 1
psaGroups_OUT(i) = PointerToStringW(lpGroups(i))
Next i
End If
'
' Clean up
'
If lpBuffer Then
Call NetApiBufferFree(lpBuffer)
End If
If nRet = NERR_Success Then
GetGlobalGroups = True
Else
GetGlobalGroups = False
End If
Exit Function
Err_GetGlobalGroups:
GetGlobalGroups = False
End Function
Public Function PointerToStringW(lpStringW As Long) As String
Dim Buffer() As Byte
Dim nLen As Long
If lpStringW Then
nLen = lstrlenW(lpStringW) * 2
If nLen Then
ReDim Buffer(0 To (nLen - 1)) As Byte
CopyMem Buffer(0), ByVal lpStringW, nLen
PointerToStringW = Buffer
End If
End If
End Function
Note: I have just lifted this straight out of some code I wrote this year without testing any of it...if I've missed anything let me know and I'll take a closer look
Good luck!
Toot
Some cause happiness wherever they go; others, whenever they go.
-
Oct 19th, 2000, 07:15 AM
#3
Thread Starter
Addicted Member
first of all ,tnx a lot..
but i need to know how to operate it ,i mean which of the function i need to put on a command_click...?
tnx again
LirLir
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 19th, 2000, 07:30 AM
#4
Thread Starter
Addicted Member
Ok i got it
i wrote something like this:
Private Sub Command1_Click()
GetGlobalGroups "lironl", groups()
Stop
End Sub
but, 4 some reason , the function returns False.
i run it step by step and fount out that on that line :
nRet = NetUserGetGroups(ByVal 0&,yUserName(0),0,pBuffer, &H4000, nRead, nTotal)
nRet is 2221
do u have any explanation ?
tnx mate,
LirLir
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 19th, 2000, 07:39 AM
#5
Lively Member
Create a new project. Add a command button called cmdGetGroups and a ListBox called lstGroups to your form.
In the Form_Load() event:
Code:
SetDomainController gsServerName
' set up the user name
If Not GetUserName(gsUserName) Then
MsgBox "ERROR: Can't get user name!", vbSystemModal Or vbCritical, "Critical error"
' and shut down
Unload Me
End
End If
' set the form's caption
Caption = App.EXEName & " - " & gsUserName & "@" & gsServerName
In the cmdGetGroups_Click() event:
Code:
Dim saGroups() As String
Dim iCount As Integer
lstGroups.Clear
If Not GetGlobalGroups(gsUserName, saGroups) Then
lstGroups.AddItem "<Can't get global group info>"
Else
' loop through results adding group names
For iCount = LBound(saGroups) To UBound(saGroups)
lstGroups.AddItem saGroups(iCount)
Next
End If
Add a module and add the following code to it:
Code:
Option Explicit
Option Base 0
' consts
Public Const UNLEN = 256 ' Maximum username length
Public Const NERR_Success As Long = 0&
' vars
Public gsUserName As String
Public gsServerName As String
'declas
Public Declare Function GetUserNameW Lib "advapi32.dll" (lpBuffer As Byte, nSize As Long) As Long
Public Declare Function NetUserGetGroups Lib "netapi32" (lpServer As Any, UserName As Byte, ByVal Level As Long, lpBuffer As Long, ByVal PrefMaxLen As Long, lpEntriesRead As Long, lpTotalEntries As Long) As Long
Public Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Public Declare Function lstrlenW Lib "KERNEL32" (ByVal lpString As Long) As Long
Public Declare Function NetApiBufferFree Lib "netapi32" (ByVal psBuffer As Long) As Long
Public Function GetUserName(ByRef psUserName_OUT As String) As Boolean
Dim Buffer As String
Dim yBuffer() As Byte
Dim nRet As Long
Dim nLen As Long
Const NameLength = UNLEN + 1
On Error GoTo Err_GetUserName
nLen = NameLength * 2
ReDim yBuffer(0 To nLen - 1) As Byte
If GetUserNameW(yBuffer(0), nLen) Then
Buffer = yBuffer
psUserName_OUT = Left(Buffer, nLen - 1)
End If
GetUserName = True
Exit Function
Err_GetUserName:
GetUserName = False
End Function
Public Function GetGlobalGroups(ByVal psUsername_IN As String, ByRef psaGroups_OUT() As String) As Boolean
Dim lpBuffer As Long
Dim yUserName() As Byte
Dim yServer() As Byte
Dim lpGroups() As Long
Dim nRead As Long
Dim nTotal As Long
Dim nRet As Long
Dim i As Long
On Error GoTo Err_GetGlobalGroups
yUserName = psUsername_IN & vbNullChar
If gsServerName = "" Then
nRet = NetUserGetGroups(ByVal 0&, yUserName(0), 0, lpBuffer, &H4000, nRead, nTotal)
Else
If InStr(gsServerName, "\\") = 1 Then
yServer = gsServerName & vbNullChar
Else
yServer = "\\" & gsServerName & vbNullChar
End If
' Debug.Print "INFO: Retrieving User groups from " & CStr(yServer)
nRet = NetUserGetGroups(yServer(0), yUserName(0), 0, lpBuffer, &H400, nRead, nTotal)
End If
If nRet = NERR_Success Then
ReDim lpGroups(0 To nRead - 1) As Long
ReDim psaGroups_OUT(0 To nRead - 1) As String
CopyMem lpGroups(0), ByVal lpBuffer, nRead * 4
For i = 0 To nRead - 1
psaGroups_OUT(i) = PointerToStringW(lpGroups(i))
Next i
End If
'
' Clean up
'
If lpBuffer Then
Call NetApiBufferFree(lpBuffer)
End If
If nRet = NERR_Success Then
GetGlobalGroups = True
Else
GetGlobalGroups = False
End If
Exit Function
Err_GetGlobalGroups:
GetGlobalGroups = False
End Function
Public Function PointerToStringW(lpStringW As Long) As String
Dim Buffer() As Byte
Dim nLen As Long
If lpStringW Then
nLen = lstrlenW(lpStringW) * 2
If nLen Then
ReDim Buffer(0 To (nLen - 1)) As Byte
CopyMem Buffer(0), ByVal lpStringW, nLen
PointerToStringW = Buffer
End If
End If
End Function
Public Sub SetDomainController(ByRef sVar_INOUT As String)
Dim sVal As String
' look for the environment variable "LOGON_SERVER"
sVal = Environ("LOGONSERVER")
If Not Len(sVal) = 0 Then
If Len(sVal) > 2 Then
If Left(sVal, 2) = "\\" Then
sVal = Right(sVal, Len(sVal) - 2)
End If
End If
' got the value - set the variable
sVar_INOUT = sVal
End If
End Sub
and away you go. Hope that explains it clearly. Sorry about the total lack of comments! My bad habits entirely.
Toot
Some cause happiness wherever they go; others, whenever they go.
-
Oct 19th, 2000, 07:51 AM
#6
Thread Starter
Addicted Member
hey toot
do u have any idea why do i get nRet=2221 and not NERR_Success ?
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 19th, 2000, 08:14 AM
#7
Lively Member
No idea at all. That error is "NERR_UserNotFound", which means The user name could not be found.
Check the user name before you're calling the GetGlobalGroups() API. Does the user exist? Are you getting the correct server? Have you tried the project I suggested?
Toot
Some cause happiness wherever they go; others, whenever they go.
-
Oct 19th, 2000, 08:41 AM
#8
Thread Starter
Addicted Member
the answer is YES i tryed your project.
1. i entered to my computer as admin
so now i get on the lst "NONE" although the admin belong
to several groups.
do you have any idea what can i do.
tnx
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 19th, 2000, 09:20 AM
#9
Lively Member
You logged into the machine as "admin"... did you log in locally (login domain = machine name) or globally (login domain = your domain)?
Step through it - does it retrieve the correct logon server and user name? Does your environment variable "USERDOMAIN" list the domain that you are logged into? Is your logon server still available when you're running this app?
Toot
Some cause happiness wherever they go; others, whenever they go.
-
Oct 19th, 2000, 09:24 AM
#10
Thread Starter
Addicted Member
yes to the last question !
The MORE I get to know,
I realize that I know NOTHING !
-
Oct 20th, 2000, 05:00 AM
#11
Lively Member
What about the other questions?
Some cause happiness wherever they go; others, whenever they go.
-
Oct 22nd, 2000, 12:15 AM
#12
Thread Starter
Addicted Member
well ,tnx mate,
i finaly succeed solving the problem.
tnx for your help
LirLir
The MORE I get to know,
I realize that I know NOTHING !
-
Aug 22nd, 2002, 11:59 AM
#13
Frenzied Member
this code only seems to work with Global Groups. Is there a way to get local group info by specifying a domain? I tried just putting in my own domain rather than the Environ("LOGONSERVER") but it can't get the info. I am typing in the correct domain... Are local groups handled differently than global groups?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|