Capture network logon name (1 Viewer)

SacValGal

Registered User.
Local time
Today, 08:53
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.
 

pdx_man

Just trying to help
Local time
Today, 08:53
Joined
Jan 23, 2001
Messages
1,347
Have you tried Ye Ole:

Environ("Username")

??
 

Banana

split with a cherry atop.
Local time
Today, 08:53
Joined
Sep 1, 2005
Messages
6,318
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.
 

SacValGal

Registered User.
Local time
Today, 08:53
Joined
Jun 16, 2005
Messages
18
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!
 

Banana

split with a cherry atop.
Local time
Today, 08:53
Joined
Sep 1, 2005
Messages
6,318
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. :)
 

Banana

split with a cherry atop.
Local time
Today, 08:53
Joined
Sep 1, 2005
Messages
6,318
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...)
 

SacValGal

Registered User.
Local time
Today, 08:53
Joined
Jun 16, 2005
Messages
18
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.
 

boblarson

Smeghead
Local time
Today, 08:53
Joined
Jan 12, 2001
Messages
32,059
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.
 

Banana

split with a cherry atop.
Local time
Today, 08:53
Joined
Sep 1, 2005
Messages
6,318
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?
 

boblarson

Smeghead
Local time
Today, 08:53
Joined
Jan 12, 2001
Messages
32,059
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.
 

Banana

split with a cherry atop.
Local time
Today, 08:53
Joined
Sep 1, 2005
Messages
6,318
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.
 

SacValGal

Registered User.
Local time
Today, 08:53
Joined
Jun 16, 2005
Messages
18
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?
 

boblarson

Smeghead
Local time
Today, 08:53
Joined
Jan 12, 2001
Messages
32,059
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.
 

SacValGal

Registered User.
Local time
Today, 08:53
Joined
Jun 16, 2005
Messages
18
True enough. I will try it, and alternatively Environ, which should be safe enough for the conditions. Thank you very much.
 

SacValGal

Registered User.
Local time
Today, 08:53
Joined
Jun 16, 2005
Messages
18
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

Top Bottom