The information in this article applies to:
- Microsoft Access versions 1.0, 1.1, 2.0
SUMMARY
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.
MORE INFORMATION
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:
- Create a new module containing the following declarations and
functions:
NOTE: In the following sample code, an underscore (_) is used as a line-
continuation character. Remove the underscore from the end of the line
when re-creating this code in Access Basic.
NOTE: You may have some Microsoft Windows API functions defined in an
existing Microsoft Access library; therefore, your declarations may be
duplicates. If you receive a duplicate procedure name error message,
remove or comment out the declarations statement in your code.
NOTE: Make sure to choose the Compile All command from the Run menu to
verify that you do not receive any compilation errors.
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
- Test this function by creating the following Access Basic 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
- In the Immediate window, type the following, and the press ENTER:
ShowInfo
Note that either a dialog box displaying information about the computer,
or, if you are running this function on a non-Windows for Workgroups
computer, an error message stating "Unable to get information" is
displayed.
REFERENCES
For an example of this article in Microsoft Access version 7.0, please see
the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q148835
TITLE: ACC: How to Retrieve Workgroup Information Under Win32
|