I want to get the users login name, and currently use a number of functions,
one of which is based on this one:
However, it doesn't work properly on a network. If the user logs into the
network as joe, and then in the local windows PC as Administrator, then that
function will bring back Administrator.
I have functionality in some applications that is only available to the
network admin, but if a user can use a local user account on their own
machine called admin, then that function will think they are the network
I have seen a bunch of different functions, including one below, which I
kindly found from a web site earlier today:
this doesn't do it either. Perhaps in a mostly windows environment, it does
not get this kind of use, but it doesn't work with for example a netware and
windows network. Does it even work with a domain login where local login is
different? What about if a user logs into a few networks at once with
different network adaptors? How can I find out a particular user name for a
particular network name?
I would really like to get this sorted once and for all, and thought this
might be the place. thanks for your help.
''''get network user name function apinetusername function - doesn't work
Public Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful
Public Function FindNetUserName() As String
' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String
' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
' An error occurred.
lpUserName = ""
FindNetUserName = lpUserName