Link to home
Start Free TrialLog in
Avatar of new_wes
new_wes

asked on

debug GetUserName - Getting username and description

hi - adopted from
https://www.experts-exchange.com/questions/21757133/Display-Windows-Username.html

CLASS Module:
Option Compare Database
Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

FORM Module:
' Return the user's name.
Private Function GUserName() As String
Const UNLEN = 256   ' Max user name length.
Dim user_name As String
Dim name_len As Integer
    name_len = Len(user_name)
    user_name = Space$(UNLEN + 1)
   
    If GetUserName(user_name, name_len) = 0 Then
        GUserName = "Unknown"
        MsgBox ("Msg1 user_name=" & user_name & ". GUserName=" & GUserName & ".")
    Else
        GUserName = Left$(user_name, name_len - 1)
        MsgBox ("Msg2 user_name=" & user_name & ". GUserName=" & GUserName & ".")
    End If
   
End Function

FORM OPEN:
Private Sub Form_Open(Cancel As Integer)
    Me.txtGUser = GUserName
End Sub

FORM OPEN RETURNS:
First messagebox and Always returns field txtGUser = "Unknown"

If i change conditional "If GetUserName..." to "> 0", then returns nothing (no msgbox etc)


1. if this works, shouldn't this ultimately return "Jane Smith" instead of ntuser "jsm057"?

2. i'm assuming that GetUserName function returns greater than zero if finds user?  either way, i can't see how this gets the user name/description into the GUserName string?

3. next step to debug??
Avatar of dqmq
dqmq
Flag of United States of America image

Try switching the order of these lines:

  name_len = Len(user_name)
  user_name = Space$(UNLEN + 1)
Avatar of new_wes
new_wes

ASKER

hmm - think i see what you mean but no effect - GetUserName always seems to return 0 . . .
SOLUTION
Avatar of Jim P.
Jim P.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of new_wes

ASKER

hi jimpen & dgmg - thanks, got both working, but both look like return same as Environ("UserName") ??  

if that's as far as we can get that's okay, just thought there would be a way to pull more from user profile??  active driectory and all etc.
You would have to develop LDAP functiions to get to that.
Avatar of new_wes

ASKER

well that counts me out!    

thanks all - tried to split points best i could -

wes
I tested your exact original code and it gave me compile errors because of the function declaration.  Here is what I use:

Declare Function GetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Note that the Environ("UserName") doesn't always populate correctly.

The way we pull it off (w/o LDAP) is that we have one centralized employee DB in access. Then we link the employees table to whatever DB we're working in.  And pull data from that.

Sorry I couldn't give a better answer. May all your days get brighter and brighter.
Avatar of new_wes

ASKER

i'll second that for all!  
Also, Environ("UserName") is not secure.  One can easily modify the environment without a password and masquerade as someone else.