Obtain the User Network Logon Name
Obtain the User Network Logon Name
Obtaining the network logon name is a commonly needed utility function for many applications and can be easily accomplished with a single call to the Win32 API suite of networking functions.
The WNetGetUser function retrieves the current default user name or the user name used to establish a network connection. This is among the simplest of API calls.
Since this function has both ANSI and Unicode versions, the function is aliased. As a matter of habit, I always prefix the Win32 API declarations with "w32_" so that I can use the actual function or sub name as my VB procedure name. Remember that API declarations are now case-sensitive.
Here's the declaration:
Private Declare Function w32_WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" ( _ ByVal lpszLocalName As String, _ ByVal lpszUserName As String, _ lpcchBuffer As Long) As LongThis function takes three parameters: lpName, lpUserName, lpnLength.
- lpszLocalName
-
Points to a null-terminated string that specifies either the name of the local
device to return the user name for or the name of a network that the user has
made a connection to.
If this parameter is NULL, Windows returns the name of the current user for the process.
If this parameter is a network name and the user is connected to that resource using different names, the network provider may not be able to resolve which user name to return. In this case, the provider may make an arbitrary choice from the possible user names. - lpszUserName
- Points to a buffer that receives the null-terminated user name.
- lpcchBuffer
- Points to a variable that specifies the size, in characters, of the buffer pointed to by lpszUserName. If the call fails because the buffer is not big enough, this variable contains the required buffer size.
Private Const NO_ERROR = 0This constant is used to test the return value of the API call.
Since lpszUserName is an output parameter of the WNetGetUser function, we'll add a simple utility function for converting null a terminated C string to a VB string. Here's that code:
Public Function CStringToVBString(psCString As String) As String
' **********
' Purpose: Convert a C string to a VB string
' Parameters: (Input Only)
' psCString - the C string to convert
' Returns: The converted VB string
' Notes:
' Returns everything to the left of the first Null character
' **********
Dim sReturn As String
Dim iNullCharPos As Integer
iNullCharPos = InStr(psCString, vbNullChar)
If iNullCharPos > 0 Then
' return everything left of the null
sReturn = Left(psCString, iNullCharPos - 1)
Else
' no null, return the original string
sReturn = psCString
End If
CStringToVBString = sReturn
End Function
Finally, here's the wrapper around Win32's WNetGetUser function:
Public Function WNetGetUser() As String
' **********
' Purpose: Retrieve the network user name
' Paramters: None
' Returns: The indicated name
' Notes:
' A zero-length string is returned if the function fails
' **********
Dim lpUserName As String
Dim lpnLength As Long
Dim lResult As Long
lpnLength = 256
lpUserName = Space(lpnLength)
lResult = w32_WNetGetUser(vbNullString, lpUserName, lpnLength)
If lResult = NO_ERROR Then
WNetGetUser = CStringToVBString(lpUserName)
Else
WNetGetUser = ""
End If
End Function
Rather than return a status code or Boolean value, the function simply returns the
value from the API call or a zero length string. Unlike some API procedures, the
probability of failure in this procedure is minimal so the extra test would only
serve to add work to any calling routine. If a possibility of failure is a vital
concern, a more robust wrapper function could be constructed.
For simplicity, I've ignored the lpszLocalName parameter in the original API call and simply passed a null string. However, it would be easy enough to restore that parameter to the wrapper function.




