Capture network logon name

SacValGal

Registered User.
Local time
Today, 10:46
Joined
Jun 16, 2005
Messages
18
Years ago, I got a nifty piece of code from someone on this forum that pulls a user's logon name from the network, so that I can stamp that name on the user's work. It works great in our office, but any time I have to develop a database for a client to use in their office, it doesn't work. I don't know what this code means, just that it works for us--so I don't know how to troubleshoot or adapt.

I don't want to use Access workgroup security on a database that I won't be present to administer.

Here's the code:

Declare Function wu_WNetGetUser Lib "advapi32.dll" Alias "GetUserNameA" (ByVal sUser As String, nBuffer As Long) As Long
Function NetWareCurUser() As String
Dim sUser As String
Dim nBu As Long
Dim X As Variant
nBu = 8
sUser = Space(nBu)
X = wu_WNetGetUser(sUser, nBu)
sUser = Left(sUser, nBu - 1)
If IsNull(sUser) Then
MsgBox "Incorrect logon"
Else
NetWareCurUser = sUser
End If
End Function

Our server is NT 4.0. What should I know about the client's network in order to adapt this routine to their environment? Or is there another approach I could try? I've reviewed posts in this forum but didn't find anything that quite addresses my question.

I'm using Access 2003.
 
Have you tried Ye Ole:

Environ("Username")

??
 
Access has a built-in function- Envir(CurrentUser) or something like that.

As for your function, are you sure that your clients has the library installed? You're referencing a library advapi32.dll; I'm not sure if this is a Windows default or a custom library associated with Netware. If it's latter, then it's your responsiblity to ensure that this is on every client's computer.
 
Thank you both!

Yes, it is Environ("UserName"). pdx_man, I missed your post and spent some time hunting the function down. I feel very silly not to have known about this, but I must say it was not so easy to find.

So, Banana, looks like I won't need to make the elaborate code sample work, but just for instance, I am so naive about this stuff, if I did need to make it work elsewhere, would it be sufficient to send them the dll and have them put it in their system folder?

This forum still rocks!
 
Generally, you may need to make sure that library is also registered, which is why several would recommend using Package Wizard to ensure that library is properly implemented while the database is being installed.

I think there was a .dll I had once where it worked without registered, but that is exceptional.


Sorry about that wild goose chase as pdx_man had it right. You can clearly see that I don't use it. :)
 
Actually, that is similar to the original code the OP posted in the OP.

The link doesn't explain how it would be safer- do you know what problems could arise with the Environ()?

(And it seems like the dll is a Windows default after all...)
 
As Banana mentions, the Dev Ashish routine is essentially what I was using and couldn't get to work on outside networks. (I'm glad to have the attribution--thanks for sourcing.) I'm inclined to give Environ() a try, but am interested in further discussion.
 
As Banana mentions, the Dev Ashish routine is essentially what I was using and couldn't get to work on outside networks. (I'm glad to have the attribution--thanks for sourcing.) I'm inclined to give Environ() a try, but am interested in further discussion.
The Environ can be hacked easily by anyone with elementary computer DOS knowledge. So, for most circumstances it can work fine. But, just be aware, anyone who knows how to set environment variables can set their network user name in there to whatever they want.
 
Thanks for that tidbit. Good to know about its weaknesses.

How would the API call circumvent this? After all, Environ() had to get it from somewhere, no?
 
Environment variables are actually different from the Windows Logon. It is normally set by Windows (I believe) when logging in, but it isn't read-only. But, the API gets the information directly from a protected area of Windows.
 
Hmmm, interesting! It seems odds that they'd have two places to put this information. Why not just have a read-only value and be done with it.

But anyway, this is good to know.
 
Any clue why the api code fails to pick up the login, if the referenced .dll is in fact standard Windows? Seems like after all there isn't anything I'd need to have them install? Does either of you see anything else that could be going wrong?
 
The code you posted is not exactly the same code as this:
Code:
'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

Try using this one explicitly.
 
True enough. I will try it, and alternatively Environ, which should be safe enough for the conditions. Thank you very much.
 
It works!

This DOES work on the client's network. Thank you so much both for getting the original code and for letting me know its source so I can cite it properly.

Yay!

SacValGal

The code you posted is not exactly the same code as this:
Code:
'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

Try using this one explicitly.
 

Users who are viewing this thread

Back
Top Bottom