Article ID: 101676
Article Last Modified on 5/6/2003
Option Explicit
Declare Function NetWkstaGetInfo% Lib "NetAPI.DLL" (ByVal lServer&, _
ByVal sLevel%, ByVal pbBuffer&, ByVal cbBuffer%, pcbTotalAvail%)
Declare Function GlobalAlloc% Lib "Kernel" (ByVal fFlags%, _
ByVal nSize&)
Declare Function GlobalLock& Lib "Kernel" (ByVal hMem%)
Declare Function GlobalUnlock% Lib "Kernel" (ByVal hMem%)
Declare Function GlobalFree% Lib "Kernel" (ByVal hMem%)
Declare Sub lstrcpy Lib "Kernel" (ByVal dest As Any, _
ByVal src As Any)
Declare Sub hmemcpy Lib "Kernel" (ByVal dest As Any, _
ByVal src As Any, ByVal size As Long)
Function GetBinInt (sobj As String, off As Integer)
If (Asc(Mid$(sobj, off + 1, 1)) < 128) Then
GetBinInt = Asc(Mid$(sobj, off, 1)) +_
Asc(Mid$(sobj, off + 1, 1)) * 256
Else
GetBinInt = ((&HFF - Asc(Mid$(sobj, off + 1, 1))) * 256) - _
Asc(Mid$(sobj, off, 1))
End If
End Function
Function GetWorkstationInfo (sComputer As String, _
sUserName As String, sWorkgroup As String, sLogonDomain As String)
Dim nRetSize As Integer, nStruct, ret As Integer, dummy As Integer
Dim hMem As Integer, lpMem As Long, sTemp As String
' Get the size of the return structure.
ret = NetWkstaGetInfo(0&, 10, 0&, 0, nRetSize)
If (ret <> 0) And (ret <> 2123) Then
GetWorkstationInfo = False
Exit Function
End If
' Allocate memory for the structure.
hMem = GlobalAlloc(0, CLng(nRetSize))
nStruct = nRetSize
If (hMem <> 0) Then
lpMem = GlobalLock(hMem)
' Read workstation information into structure.
ret = NetWkstaGetInfo(0&, 10, lpMem, nRetSize, nRetSize)
If (ret = 0) Then
sTemp = Space(100)
hmemcpy sTemp, lpMem, nStruct
' Retrieve the computer name.
sComputer = Space(100)
lpMem = CLng(GetBinInt(sTemp, 1)) +_
(CLng(GetBinInt(sTemp, 3)) * CLng(&H10000))
lstrcpy sComputer, lpMem
sComputer = Mid(sComputer, 1, InStr(sComputer, Chr(0)) - 1)
' Retrieve the user name.
sUserName = Space(100)
lpMem = CLng(GetBinInt(sTemp, 5)) +_
(CLng(GetBinInt(sTemp, 7)) * &H10000)
lstrcpy sUserName, lpMem
sUserName = Mid(sUserName, 1, InStr(sUserName, Chr(0)) - 1)
' Retrieve the workgroup name.
sWorkgroup = Space(100)
lpMem = CLng(GetBinInt(sTemp, 9)) +_
(CLng(GetBinInt(sTemp, 11)) * &H10000)
lstrcpy sWorkgroup, lpMem
sWorkgroup = Mid(sWorkgroup, 1, InStr(sWorkgroup, _
Chr(0)) - 1)
' Retrieve the logon domain name.
sLogonDomain = Space(100)
lpMem = CLng(GetBinInt(sTemp, 15)) +_
(CLng(GetBinInt(sTemp, 17)) * &H10000)
lstrcpy sLogonDomain, lpMem
sLogonDomain = Mid(sLogonDomain, 1, InStr(sLogonDomain, _
Chr(0)) - 1)
End If
' Free the memory allocated.
dummy = GlobalUnlock(hMem)
dummy = GlobalFree(hMem)
Else
ret = -1
End If
GetWorkstationInfo = IIf(ret = 0, True, False)
End Function
Sub ShowInfo ()
Dim a$, b$, c$, d$
If GetWorkstationInfo(a$, b$, c$, d$) Then
MsgBox a$ & " " & b$ & " " & c$ & " " & d$
Else
MsgBox "Unable to get information."
End If
End Sub
Additional query words: user name networks
Keywords: kbhowto kbprogramming KB101676