Microsoft KB Archive/101676: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 69: Line 69:
</p>
</p>
<pre class="fixed_text">      Option Explicit
<pre class="fixed_text">      Option Explicit
       Declare Function NetWkstaGetInfo% Lib "NetAPI.DLL" (ByVal lServer&amp;, _
       Declare Function NetWkstaGetInfo% Lib "NetAPI.DLL" (ByVal lServer&, _
         ByVal sLevel%, ByVal pbBuffer&amp;, ByVal cbBuffer%, pcbTotalAvail%)
         ByVal sLevel%, ByVal pbBuffer&, ByVal cbBuffer%, pcbTotalAvail%)
       Declare Function GlobalAlloc% Lib "Kernel" (ByVal fFlags%, _
       Declare Function GlobalAlloc% Lib "Kernel" (ByVal fFlags%, _
         ByVal nSize&amp;)
         ByVal nSize&)
       Declare Function GlobalLock&amp; Lib "Kernel" (ByVal hMem%)
       Declare Function GlobalLock& Lib "Kernel" (ByVal hMem%)
       Declare Function GlobalUnlock% Lib "Kernel" (ByVal hMem%)
       Declare Function GlobalUnlock% Lib "Kernel" (ByVal hMem%)
       Declare Function GlobalFree% Lib "Kernel" (ByVal hMem%)
       Declare Function GlobalFree% Lib "Kernel" (ByVal hMem%)
Line 86: Line 86:
                             Asc(Mid$(sobj, off + 1, 1)) * 256
                             Asc(Mid$(sobj, off + 1, 1)) * 256
         Else
         Else
             GetBinInt = ((&amp;HFF - Asc(Mid$(sobj, off + 1, 1))) * 256) - _
             GetBinInt = ((&HFF - Asc(Mid$(sobj, off + 1, 1))) * 256) - _
                           Asc(Mid$(sobj, off, 1))
                           Asc(Mid$(sobj, off, 1))
         End If
         End If
Line 97: Line 97:


         ' Get the size of the return structure.
         ' Get the size of the return structure.
         ret = NetWkstaGetInfo(0&amp;, 10, 0&amp;, 0, nRetSize)
         ret = NetWkstaGetInfo(0&, 10, 0&, 0, nRetSize)
         If (ret <&gt; 0) And (ret <&gt; 2123) Then
         If (ret <> 0) And (ret <> 2123) Then
             GetWorkstationInfo = False
             GetWorkstationInfo = False
             Exit Function
             Exit Function
Line 106: Line 106:
         hMem = GlobalAlloc(0, CLng(nRetSize))
         hMem = GlobalAlloc(0, CLng(nRetSize))
         nStruct = nRetSize
         nStruct = nRetSize
         If (hMem <&gt; 0) Then
         If (hMem <> 0) Then
             lpMem = GlobalLock(hMem)
             lpMem = GlobalLock(hMem)


             ' Read workstation information into structure.
             ' Read workstation information into structure.
             ret = NetWkstaGetInfo(0&amp;, 10, lpMem, nRetSize, nRetSize)
             ret = NetWkstaGetInfo(0&, 10, lpMem, nRetSize, nRetSize)
             If (ret = 0) Then
             If (ret = 0) Then
               sTemp = Space(100)
               sTemp = Space(100)
Line 118: Line 118:
               sComputer = Space(100)
               sComputer = Space(100)
               lpMem = CLng(GetBinInt(sTemp, 1)) +_
               lpMem = CLng(GetBinInt(sTemp, 1)) +_
                           (CLng(GetBinInt(sTemp, 3)) * CLng(&amp;H10000))
                           (CLng(GetBinInt(sTemp, 3)) * CLng(&H10000))
               lstrcpy sComputer, lpMem
               lstrcpy sComputer, lpMem
               sComputer = Mid(sComputer, 1, InStr(sComputer, Chr(0)) - 1)
               sComputer = Mid(sComputer, 1, InStr(sComputer, Chr(0)) - 1)
Line 125: Line 125:
               sUserName = Space(100)
               sUserName = Space(100)
               lpMem = CLng(GetBinInt(sTemp, 5)) +_
               lpMem = CLng(GetBinInt(sTemp, 5)) +_
                           (CLng(GetBinInt(sTemp, 7)) * &amp;H10000)
                           (CLng(GetBinInt(sTemp, 7)) * &H10000)
               lstrcpy sUserName, lpMem
               lstrcpy sUserName, lpMem
               sUserName = Mid(sUserName, 1, InStr(sUserName, Chr(0)) - 1)
               sUserName = Mid(sUserName, 1, InStr(sUserName, Chr(0)) - 1)
Line 132: Line 132:
               sWorkgroup = Space(100)
               sWorkgroup = Space(100)
               lpMem = CLng(GetBinInt(sTemp, 9)) +_
               lpMem = CLng(GetBinInt(sTemp, 9)) +_
                           (CLng(GetBinInt(sTemp, 11)) * &amp;H10000)
                           (CLng(GetBinInt(sTemp, 11)) * &H10000)
               lstrcpy sWorkgroup, lpMem
               lstrcpy sWorkgroup, lpMem
               sWorkgroup = Mid(sWorkgroup, 1, InStr(sWorkgroup, _
               sWorkgroup = Mid(sWorkgroup, 1, InStr(sWorkgroup, _
Line 140: Line 140:
               sLogonDomain = Space(100)
               sLogonDomain = Space(100)
               lpMem = CLng(GetBinInt(sTemp, 15)) +_
               lpMem = CLng(GetBinInt(sTemp, 15)) +_
                           (CLng(GetBinInt(sTemp, 17)) * &amp;H10000)
                           (CLng(GetBinInt(sTemp, 17)) * &H10000)
               lstrcpy sLogonDomain, lpMem
               lstrcpy sLogonDomain, lpMem
               sLogonDomain = Mid(sLogonDomain, 1, InStr(sLogonDomain, _
               sLogonDomain = Mid(sLogonDomain, 1, InStr(sLogonDomain, _
Line 160: Line 160:


         If GetWorkstationInfo(a$, b$, c$, d$) Then
         If GetWorkstationInfo(a$, b$, c$, d$) Then
             MsgBox a$ &amp; " " &amp; b$ &amp; " " &amp; c$ &amp; " " &amp; d$
             MsgBox a$ & " " & b$ & " " & c$ & " " & d$
         Else
         Else
             MsgBox "Unable to get information."
             MsgBox "Unable to get information."

Latest revision as of 12:24, 21 July 2020

Knowledge Base


ACC: How to Retrieve Windows for Workgroups User Information

Article ID: 101676

Article Last Modified on 5/6/2003



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition



This article was previously published under Q101676

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:

  1. 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
                            
  2. 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
                            
  3. 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:

148835 ACC: How to Retrieve Workgroup Information Under Win32


Additional query words: user name networks

Keywords: kbhowto kbprogramming KB101676