Newsgroup: it.comp.lang.visual-basic
Titolo: [?]Utenti e gruppi NT

Nel computer managment di un server NT  l' amministratore definisce dei
'Gruppi' (che rappresentano i livelli di accesso ad un database SQL ) e poi
degli 'utenti' che vengono associati ai vari Gruppi.
Da una applicazione VB (che gira su un qualunque computer della rete) con la
funzione
<Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long>
io posso ricavarmi il nome dell'utente che si è loggato. E' possibile
ricavare il nome del  gruppo (o dei gruppi) in cui è inserito?
Grazie

 Thanks in advance!
Attribute VB_Name = "ModzUserCompOS"
Option Explicit
Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128      '  Maintenance string for PSS
usage
End Type
Public Enum WinVersions
    Win_Unknown = 0
    win_95
    win_98
    win_ME
    Win_NT4
    Win_NT3
    win_2000
    Win_XP
    'Win_CE
End Enum
'geting compname, user and window version is a breeze
Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias
"GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
'but the DOMAIN NAME turned out to be a bitch, with a win9x/nt conflict.
Private Declare Function NetWkstaUserGetInfo& Lib "Netapi32" (Reserved
As Any, ByVal lLevel&, pbBuffer As Any)
Private Declare Sub lstrcpyW Lib "kernel32" (dest As Any, ByVal src As
Any)
Private Declare Sub lstrcpy Lib "kernel32" (dest As Any, ByVal src As
Any)
Private Declare Sub RtlMoveMemory Lib "kernel32" (dest As Any, src As
Any, ByVal size&)
Private Declare Function NetApiBufferFree& Lib "Netapi32" (ByVal
buffer&)
Private Type WKSTA_USER_INFO_1
    wkui1_username As Long
    wkui1_logon_domain As Long
    wkui1_logon_server As Long
    wkui1_oth_domains As Long
End Type
Private Type WKSTA_INFO_100
    wki100_platform_id As Long
    wki100_computername As String
    wki100_langroup As String
    wki100_ver_major As Long
    wki100_ver_minor As Long
End Type
Public Property Get UserName() As String
    Const ULEN As Long = 257
    Dim s As String * ULEN, l As Long
    GetUserName s, ULEN
    l = InStr(1, s, Chr(0), vbBinaryCompare)
    If l > 1 Then
        UserName = Left$(s, l - 1)
    End If
End Property
Public Property Get ComputerName() As String
    Const ULEN As Long = 257
    Dim s As String * ULEN, l As Long
    GetComputerName s, ULEN
    l = InStr(1, s, Chr(0), vbBinaryCompare)
    If l > 1 Then
        ComputerName = Left$(s, l - 1)
    End If
End Property
Public Property Get DomainName() As String
        Dim logondomain As String, ver As WinVersions
        ver = WinVersion
    Select Case ver
        Case win_95, win_98, win_ME
            ' The following section works only under Windows 9x/me
            'depending on the "Logon to WinNT domain" check box...
            Dim bb As Long
            bb = RegGetLong(HKEY_LOCAL_MACHINE, "Network\Logon",
"LMLogon")
            If CBool(bb) = True Then
                'use the "Domain name"
                logondomain = RegGetString(HKEY_LOCAL_MACHINE,
"System\CurrentControlSet\Services\MSNP32\NetworkProvider",
"AuthenticatingAgent")
                MsgBox "Auth agent returnd = " & logondomain
            Else
                'use "workgroup" name
                logondomain = RegGetString(HKEY_LOCAL_MACHINE,
"System\CurrentControlSet\Services\VxD\VNETSUP", "Workgroup")
            End If
        Case win_2000, Win_NT4, Win_XP
            ' The following section works only under Windows NT/2k/xp
            Dim buffer(512) As Byte, i As Long
            Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
            Call NetWkstaUserGetInfo(ByVal 0&, 1, pwk1)
            RtlMoveMemory wk1, ByVal pwk1, Len(wk1)
            lstrcpyW buffer(0), wk1.wkui1_logon_domain
            i = 0
            Do While buffer(i) <> 0
               logondomain = logondomain & Chr(buffer(i))
               i = i + 2
            Loop
            Call NetApiBufferFree(pwk1)
        Case Else
            'return ""
            logondomain = ""
    End Select
    DomainName = logondomain
End Property
Public Property Get WinVersionStr() As String
    Dim v As WinVersions
    v = WinVersion
    Select Case v
        Case win_2000: WinVersionStr = "Windows 2000"
        Case win_95: WinVersionStr = "Windows 95"
        Case win_98: WinVersionStr = "Windows 98"
        Case win_ME: WinVersionStr = "Windows ME"
        Case Win_NT3: WinVersionStr = "Windows NT3"
        Case Win_NT4: WinVersionStr = "Windows NT4"
        Case Win_XP: WinVersionStr = "Windows XP"
        Case Else: WinVersionStr = "Unknown OS"
    End Select
End Property
Public Property Get WinVersion() As WinVersions
    Dim t As OSVERSIONINFO
    Const VER_PLATFORM_WIN32s As Long = 0 'Win32s on Windows 3.1.
    Const VER_PLATFORM_WIN32_WINDOWS As Long = 1 'Windows 95, Windows
98, or Windows Me.
    Const VER_PLATFORM_WIN32_NT As Long = 2 'Windows NT 4.0, Windows
2000, or Whistler.
    'Const VER_PLATFORM_WIN32_CE As Long = 3 'this is just a guess, not
documented (yet)
    Const Minor_95 As Long = 0
    Const Minor_98 As Long = 10
    Const Minor_Me As Long = 90
    Const Minor_NT3 As Long = 51
    Const Minor_NT4 As Long = 0
    Const Minor_2000 As Long = 0
    Const Minor_XP As Long = 1
    Const Major_95 As Long = 4
    Const Major_98 As Long = 4
    Const Major_ME As Long = 4
    Const Major_NT3 As Long = 3
    Const Major_NT4 As Long = 4
    Const Major_2000 As Long = 5
    Const Major_XP As Long = 5
    t.dwOSVersionInfoSize = Len(t)
    If GetVersionEx(t) <> 0 Then
        With t
            If .dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
                '95,98,me
                If .dwMinorVersion = Minor_95 Then
                    '95
                    WinVersion = win_95
                ElseIf .dwMinorVersion = Minor_98 Then
                    '98
                    WinVersion = win_98
                ElseIf .dwMinorVersion = Minor_Me Then
                    'me
                    WinVersion = win_ME
                Else
                    'unknown
                    WinVersion = Win_Unknown
                End If
            ElseIf .dwPlatformId = VER_PLATFORM_WIN32_NT Then
                'nt4,2k,xp
                If .dwMajorVersion = Major_2000 Then
                    'xp,2k
                    If .dwMinorVersion = Minor_XP Then
                        'xp
                        WinVersion = Win_XP
                    Else
                        '2k
                        WinVersion = win_2000
                    End If
                Else
                    'unknown
                    WinVersion = Win_Unknown
                End If
            ElseIf .dwPlatformId = VER_PLATFORM_WIN32s Then
                'assume win3.x
                WinVersion = Win_NT3
            Else
                'unknown
                WinVersion = Win_Unknown
            End If
        End With
    Else
        'call failed
        WinVersion = Win_Unknown
    End If
End Property

Grazie a Pal per le informazioni (utili in ogni caso!) ma a me serve trovare
il 'Gruppo' a cui l'utente appartiene e non il 'Dominio'.