• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

debug GetUserName - Getting username and description

hi - adopted from
http://www.experts-exchange.com/Databases/MS_Access/Q_21757133.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??
0
new_wes
Asked:
new_wes
  • 4
  • 4
  • 3
2 Solutions
 
dqmqCommented:
Try switching the order of these lines:

  name_len = Len(user_name)
  user_name = Space$(UNLEN + 1)
0
 
new_wesAuthor Commented:
hmm - think i see what you mean but no effect - GetUserName always seems to return 0 . . .
0
 
Jim P.Commented:
This is the one I use. It returns the WinNT user name.
------------------------------------------------------
Option Compare Database
Option Explicit

'From http://support.microsoft.com/default.aspx?scid=kb;en-us;161394
 ' Declare for call to mpr.dll.
   Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUsersName As String, lpnLength As Long) As Long

   Const NoError = 0       'The Function call was successful

Public Function GetUsersName() 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, lpUsersName As String

      ' Assign the buffer size constant to lpUsersName.
      lpUsersName = Space$(lpnLength + 1)

      ' Get the log-on name of the person using product.
      Status = WNetGetUser(lpName, lpUsersName, 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 used
         ' cleanly in Visual Basic.
         lpUsersName = Left$(lpUsersName, InStr(lpUsersName, Chr(0)) - 1)
      Else

         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      End If

      ' Display the name of the person logged on to the machine.
'      MsgBox "The person logged on this machine is: " & lpUsersName
    GetUsersName = lpUsersName
End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dqmqCommented:
Examining closer, isn't this a little tighter:


Private Function GUserName() As String
Dim user_name As String * 257
    If GetUserName(user_name, len(user_name)) > 0 Then
         GUserName = Left$(user_name, InStr(1, user_name, vbNullChar) - 1)
     else
        GUserName = "Unknown"
    End If  
    MsgBox ("Msg2 user_name=" & user_name & ". GUserName=" & GUserName & ".")
End Function

     
0
 
new_wesAuthor Commented:
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.
0
 
Jim P.Commented:
You would have to develop LDAP functiions to get to that.
0
 
new_wesAuthor Commented:
well that counts me out!    

thanks all - tried to split points best i could -

wes
0
 
dqmqCommented:
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

0
 
Jim P.Commented:
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.
0
 
new_wesAuthor Commented:
i'll second that for all!  
0
 
dqmqCommented:
Also, Environ("UserName") is not secure.  One can easily modify the environment without a password and masquerade as someone else.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now