Microsoft KB Archive/210088

= ACC2000: How to Retrieve Workgroup Information Under Win32 =

Article ID: 210088

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210088



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
In Microsoft Windows NT or Microsoft Windows 2000, you can use the Win32 application programming interface (API) to retrieve network information about the currently running computer, such as the user name, workgroup, domain, and computer name. In Microsoft Windows 95/98, you can use Visual Basic for Applications code to retrieve similar information.



MORE INFORMATION
NetWkstaGetInfo, a Windows Application Programming Interface (API), takes advantage of the Windows NT and Windows 2000 security model and returns the computer, workgroup, and domain. Windows 95 and Windows 98, however, can get the current user name with the network independent function call WNetGetUser and can get the current computer name with the function GetComputerName. The following code samples demonstrate the use of all three.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To retrieve network information using Visual Basic and the Win32 API, follow these steps:  Start Microsoft Access and open any database.  Create a module and type or paste the following lines at the top: Option Explicit Type WKSTA_INFO_101 wki101_platform_id As Long wki101_computername As Long wki101_langroup As Long wki101_ver_major As Long wki101_ver_minor As Long wki101_lanroot As Long End Type

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

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _ (lpName As Any, ByVal lpUserName$, lpnLength&) Declare Function NetWkstaGetInfo& Lib "Netapi32" _ (strServer As Any, ByVal lLevel&, pbBuffer As Any) Declare Function NetWkstaUserGetInfo& Lib "Netapi32" _ (reserved As Any, ByVal lLevel&, pbBuffer As Any) Declare Sub lstrcpyW Lib "Kernel32" (dest As Any, ByVal src As Any) Declare Sub lstrcpy Lib "Kernel32" (dest As Any, ByVal src As Any) Declare Sub RtlMoveMemory Lib "Kernel32" _ (dest As Any, src As Any, ByVal size&) Declare Function NetApiBufferFree& Lib "Netapi32" (ByVal buffer&) 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: All Declare statements must be typed exactly as shown, including capitalization, because Win32 names are case-sensitive. To help eliminate errors by you or others who use your Declare statements, create Alias clauses for Declare statements that do not have an existing Alias. As long as the Alias is correctly spelled and capitalized, it does not matter how the function name is capitalized.

NOTE: Make sure to click the Compile All command on the Run menu to verify that you do not receive any compilation errors.   Below the declarations, paste or type the following procedure: Function GetWorkstationInfo Dim ret As Long, buffer(512) As Byte, i As Integer Dim wk101 As WKSTA_INFO_101, pwk101 As Long Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long Dim cbusername As Long, username As String Dim computername As String, langroup As String, logondomain As _ String

' Clear all of the display values. computername = "": langroup = "": username = "": logondomain = ""

' Windows 95 or NT - call WNetGetUser to get the name of the user. username = Space(256) cbusername = Len(username) ret = WNetGetUser(ByVal 0&, username, cbusername) If ret = 0 Then ' Success - strip off the null. username = Left(username, InStr(username, Chr(0)) - 1) Else username = "" End If

'================================================================== ' The following section works only under Windows NT or Windows 2000 '==================================================================

'NT only - call NetWkstaGetInfo to get computer name and lan group ret = NetWkstaGetInfo(ByVal 0&, 101, pwk101) RtlMoveMemory wk101, ByVal pwk101, Len(wk101) lstrcpyW buffer(0), wk101.wki101_computername ' Get every other byte from Unicode string. i = 0 Do While buffer(i) <> 0 computername = computername & Chr(buffer(i)) i = i + 2 Loop lstrcpyW buffer(0), wk101.wki101_langroup i = 0 Do While buffer(i) <> 0 langroup = langroup & Chr(buffer(i)) i = i + 2 Loop ret = NetApiBufferFree(pwk101)

' NT only - call NetWkstaUserGetInfo. ret = NetWkstaUserGetInfo(ByVal 0&, 1, pwk1) RtlMoveMemory wk1, ByVal pwk1, Len(wk1) lstrcpyW buffer(0), wk1.wkui1_logon_domain i = 0 Do While buffer(i) <> 0 logondomain = logondomain & Chr(buffer(i)) i = i + 2 Loop ret = NetApiBufferFree(pwk1)

'================================================================ 'End NT/Windows 2000-specific section '================================================================

debug.print computername, langroup, username, logondomain End Function   To test this function, type the following line in the Immediate window, and then press ENTER: GetWorkstationInfo You next see your computer's data displayed in the Immediate Window. 

To retrieve the current computer name under Windows 95 and Windows 98, using Visual Basic for Applications and the Win32 API, follow these steps:   Create a module and paste or type the following code: Option Explicit

Private Declare Function GetComputerName _ Lib "kernel32" Alias "GetComputerNameA" ( _ ByVal lpBuffer As String, nSize As Long) As Long

Private Const MAX_COMPUTERNAME_LENGTH As Long = 15&

Public Function CurrentMachineName As String Dim lSize As Long Dim sBuffer As String sBuffer = Space$(MAX_COMPUTERNAME_LENGTH + 1) lSize = Len(sBuffer)

If GetComputerName(sBuffer, lSize) Then CurrentMachineName = Left$(sBuffer, lSize) End If

End Function </li>  To test this function, type the following line in the Immediate window, and then press ENTER: ?CurrentMachineName You next see your computer's data displayed in the Immediate window. </li></ol>

Keywords: kbhowto kbapi kbnetwork kbprogramming KB210088

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.