Link to home
Start Free TrialLog in
Avatar of marcrrobitaille
marcrrobitaille

asked on

LookupAccountName

I am building an application that needs to know who is loggin on a computer link to a network.  It is a NT 4.0 network.  I use the GetUserName API function and it work's Ok.  My problem is when 2 users who have the same name but identify by 2 differents domains serveur, how can i check this ? i try whit the LookupAccountName function but it doesn't work.
Could you tell me how to acess the domains names and contents from the NT serveur ...from a VB5 application. (API ?)
Avatar of marcrrobitaille
marcrrobitaille

ASKER

Edited text of question
You might want to try the NetUserGetInfo API.  There is a microsoft article describing the call to this API.  You might want to read this article at
http://support.microsoft.com/support/kb/articles/q151/7/74.asp
Hope this answers your question.
Use the following class, yu will retreive a lot of useful informations on your users (Username, computername, domain, IP...)


Option Explicit

'
' Win32 APIs to determine OS information.
'
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128
End Type
Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

'
' Win32 NetAPIs.
'
Private Declare Function NetWkstaGetInfo Lib "Netapi32.dll" (lpServer As Any, ByVal Level As Long, lpBuffer As Any) As Long
Private Declare Function NetWkstaUserGetInfo Lib "Netapi32.dll" (ByVal Reserved As Any, ByVal Level As Long, lpBuffer As Any) As Long
Private Declare Function NetAPIBufferFree Lib "Netapi32.dll" Alias "NetApiBufferFree" (ByVal lpBuffer As Long) As Long

'
' Data handling APIs
'
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function lstrcpyW Lib "kernel32" (lpString1 As Byte, ByVal lpString2 As Long) As Long

Private Type WKSTA_INFO_102
   wki102_platform_id As Long
   wki102_computername As Long
   wki102_langroup As Long
   wki102_ver_major As Long
   wki102_ver_minor As Long
   wki102_lanroot As Long
   wki102_logged_on_users As Long
End Type

Private Type WkstaInfo102
   PlatformId As Long
   ComputerName As String
   LanGroup As String
   VerMajor As Long
   VerMinor As Long
   LanRoot As String
   LoggedOnUsers As Long
End Type

Private Type WKSTA_USER_INFO_1
   wkui1_username As Long
   wkui1_logon_domain As Long
   wkui1_oth_domains As Long
   wkui1_logon_server As Long
End Type

Private Type WkstaUserInfo1
   UserName As String
   LogonDomain As String
   OtherDomains As String
   LogonServer As String
End Type

Private Const NERR_Success As Long = 0&
'
' Member variables
'
Private m_Wks As WkstaInfo102
Private m_User As WkstaUserInfo1
Private m_IsWinNT As Boolean
   
' *********************************************************
'  Initialization
' *********************************************************
Private Sub Class_Initialize()
   Dim os As OSVERSIONINFO
   '
   ' Check to make sure we're running NT!
   '
   os.dwOSVersionInfoSize = Len(os)
   Call GetVersionEx(os)
   If os.dwPlatformId = VER_PLATFORM_WIN32_NT Then
      m_IsWinNT = True
      Me.Refresh
   End If

   SocketsInitialize

End Sub

' *********************************************************
'  Public Properties (Workstation)
' *********************************************************
Public Property Get ComputerName() As String
   ComputerName = m_Wks.ComputerName
End Property

Public Property Get Domain() As String
   Domain = m_Wks.LanGroup
End Property

Public Property Get LanRoot() As String
   LanRoot = m_Wks.LanRoot
End Property

Public Property Get LoggedOnUsers() As Long
   LoggedOnUsers = m_Wks.LoggedOnUsers
End Property

Public Property Get PlatformId() As Long
   PlatformId = m_Wks.PlatformId
End Property

Public Property Get VerMajor() As Long
   VerMajor = m_Wks.VerMajor
End Property

Public Property Get VerMinor() As Long
   VerMinor = m_Wks.VerMinor
End Property

' *********************************************************
'  Public Properties (Workstation User)
' *********************************************************
Public Property Get LogonDomain() As String
   LogonDomain = m_User.LogonDomain
End Property

Public Property Get LogonServer() As String
   LogonServer = m_User.LogonServer
End Property

Public Property Get OtherDomains() As String
   OtherDomains = m_User.OtherDomains
End Property

Public Property Get UserName() As String
   UserName = m_User.UserName
End Property

Public Property Get IPAdress() As String
   IPAdress = GetIPAdress()
End Property

' *********************************************************
'  Public Methods
' *********************************************************
Public Sub Refresh()
   Dim lpBuffer As Long
   Dim nRet As Long
   Dim wki As WKSTA_INFO_102
   Dim wkui As WKSTA_USER_INFO_1
   '
   ' These functions only exist in Windows NT!!!
   '
   If Not m_IsWinNT Then Exit Sub
   '
   ' Obtain workstation information
   '
   nRet = NetWkstaGetInfo(ByVal 0&, 102&, lpBuffer)
   If nRet = NERR_Success Then
      '
      ' Transfer data to VB-friendly structure
      '
      CopyMem wki, ByVal lpBuffer, Len(wki)
      m_Wks.PlatformId = wki.wki102_platform_id
      m_Wks.ComputerName = PointerToStringW(wki.wki102_computername)
      m_Wks.LanGroup = PointerToStringW(wki.wki102_langroup)
      m_Wks.VerMajor = wki.wki102_ver_major
      m_Wks.VerMinor = wki.wki102_ver_minor
      m_Wks.LanRoot = PointerToStringW(wki.wki102_lanroot)
      m_Wks.LoggedOnUsers = wki.wki102_logged_on_users
      '
      ' Clean up
      '
      If lpBuffer Then
         Call NetAPIBufferFree(lpBuffer)
      End If
   End If
   '
   ' Obtain user information for this workstation
   '
   nRet = NetWkstaUserGetInfo(0&, 1&, lpBuffer)
   If nRet = NERR_Success Then
      '
      ' Transfer data to VB-friendly structure
      '
      CopyMem wkui, ByVal lpBuffer, Len(wkui)
      m_User.UserName = PointerToStringW(wkui.wkui1_username)
      m_User.LogonDomain = PointerToStringW(wkui.wkui1_logon_domain)
      m_User.OtherDomains = PointerToStringW(wkui.wkui1_oth_domains)
      m_User.LogonServer = PointerToStringW(wkui.wkui1_logon_server)
      '
      ' Clean up
      '
      If lpBuffer Then
         Call NetAPIBufferFree(lpBuffer)
      End If
   End If
End Sub

' *********************************************************
'  Private Methods
' *********************************************************
Private 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

Private Sub Class_Terminate()
   SocketsCleanup
End Sub

I try a lot of things. But i still have a problem.  The Netapi32.dll doesn't works at all.  The network OS is NT but the station OS where the application run is Win 95.
Sure, it wil only work under NT
What is SocketsInitialize.  I have a compile error,Sub or function not defined
Oops, I forgot to add this function :

' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 25/09/98
' * Time             : 13:51
' * Module Name      : IP_Module
' * Module Filename  : IP.bas
' **********************************************************************
' * Comments         : Get IP Adress of the computer
' *
' *
' **********************************************************************

Option Explicit

Private Const WS_VERSION_READ = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_READ \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_READ And &HFF&
Private Const MIN_SOCKETS_READ = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128

Private Type HOSTENT
   hName          As Long
   hAliases       As Long
   hAddrType      As Integer
   hLength        As Integer
   hAddrList      As Long
End Type

Private Type WSADATA
   wVersion                               As Integer
   wHighVersion                           As Integer
   szDescription(0 To WSADescription_Len) As Byte
   szSystemStatus(0 To WSASYS_Status_Len) As Byte
   iMaxSockets                            As Integer
   iMaxUdpDg                              As Integer
   lpszVendorInfo                         As Long
End Type

Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, HostLen&) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long

Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

Function HiByte(ByVal wParam As Integer)
   HiByte = wParam \ &H100 And &HFF&
End Function

Function LoByte(ByVal wParam As Integer)
   LoByte = wParam And &HFF&
End Function

Public Sub SocketsInitialize()

   Dim WSAD As WSADATA
   Dim iReturn As Integer
   Dim sLowByte As String, sHighByte As String, sMsg As String

   iReturn = WSAStartup(WS_VERSION_READ, WSAD)

   If iReturn <> 0 Then
      MsgBox "Winsock.dll is not responding."
      End
   End If

   If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
      sHighByte = Trim$(str$(HiByte(WSAD.wVersion)))
      sLowByte = Trim$(str$(LoByte(WSAD.wVersion)))
      sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
      sMsg = sMsg & " is not supported by winsock.dll "
      MsgBox sMsg
      End
   End If

   If WSAD.iMaxSockets < MIN_SOCKETS_READ Then
      sMsg = "This application requires a minimum of "
      sMsg = sMsg & Trim$(str$(MIN_SOCKETS_READ)) & " supported sockets."
      MsgBox sMsg
      End
   End If

End Sub

Public Sub SocketsCleanup()
   Dim lReturn As Long

   lReturn = WSACleanup()

   If lReturn <> 0 Then
      MsgBox "Socket error " & Trim$(str$(lReturn)) & " occurred in Cleanup """
      End
   End If

End Sub

Public Function GetIPAdress() As String
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 25/09/98
   ' * Time             : 13:51
   ' * Module Name      : IP_Module
   ' * Module Filename  : IP.bas
   ' * Procedure Name   : GetIPAdress
   ' * Parameters       :
   ' **********************************************************************
   ' * Comments         : Get the actual IP Adress
   ' *
   ' *
   ' **********************************************************************

   Dim hostname            As String * 256
   Dim hostent_addr        As Long
   Dim host                As HOSTENT
   Dim hostip_addr         As Long
   Dim temp_ip_address()   As Byte
   Dim i                   As Integer
   Dim ip_address          As String

   If gethostname(hostname, 256) = SOCKET_ERROR Then
      MsgBox "Windows Sockets error " & str(WSAGetLastError())
      Exit Function
   Else
      hostname = Trim$(hostname)
   End If

   hostent_addr = gethostbyname(hostname)

   If hostent_addr = 0 Then
      MsgBox "Winsock.dll is not responding."
      Exit Function
   End If

   RtlMoveMemory host, hostent_addr, LenB(host)
   RtlMoveMemory hostip_addr, host.hAddrList, 4

   ReDim temp_ip_address(1 To host.hLength)
   RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

   For i = 1 To host.hLength
      ip_address = ip_address & temp_ip_address(i) & "."
   Next
   ip_address = Mid(ip_address, 1, Len(ip_address) - 1)

   GetIPAdress = ip_address

End Function


ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial