ACC: How to Retrieve Windows for Workgroups User InformationID: Q101676
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
Using the Microsoft Windows for Workgroups application programming
interface (API), you can retrieve user information, such as user name,
workgroup, domain, and computer name, about the currently running computer.
Windows for Workgroups includes a function called NetWkstaGetInfo()
that returns information about a computer. This function is especially
useful for multiuser applications.
The following procedure explains how to use Access Basic and several
Windows API calls to gain access to the NetWkstaGetInfo() function:
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
For an example of this article in Microsoft Access version 7.0, please see
the following article in the Microsoft Knowledge Base:
Q148835 ACC: How to Retrieve Workgroup Information Under Win32
Additional query words: user name networks
Keywords : kbprg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 24, 1999