Results 1 to 14 of 14

Thread: Current User Name

  1. #1

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61

    Current User Name

    Hello,

    I'd like to extract the current logged in username (%username%)into a string, then insert it into a specific line in a text file. Any help would be appreciated.

    Thanks,
    Gabe

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private UserAccount As String
    4.  
    5. Private Sub GetUserAccount()
    6.  
    7.     Dim sBuffer As String
    8.     Dim lSize As Long
    9.    
    10.     sBuffer = Space$(255)
    11.     lSize = Len(sBuffer)
    12.     Call GetUserName(sBuffer, lSize)
    13.     If lSize > 0 Then
    14.         UserAccount = Left$(sBuffer, lSize - 1)
    15.     Else
    16.         UserAccount = vbNullString
    17.     End If
    18.    
    19. End Sub
    20.  
    21. Private Sub Command1_Click()
    22. Text1.Text = "Here is the current logged on user:  " & UserAccount
    23. End Sub

  3. #3

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    Thanks for the reply, but I can't make that work, or at least it isn't getting my current username.

    I saw something called NPGetUser in the MSDN, but I've no idea how to write that into code that works. Any ideas there?

    Thanks,
    Gabe

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    ????? What OS are you using?

    I've used the code on Win9x, NT, W2K, and never had a problem.

  5. #5

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    XP. I'll try it on a 2000 box, because that's what it's going to be used on anyway.

  6. #6

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    Just tried it. Didn't work on 2000 either.

  7. #7
    Addicted Member
    Join Date
    May 2002
    Location
    Rome, Italy
    Posts
    150
    He is correct Mr. Hack, it does not work on Windows 2000. How do to this on Windows 2000?
    Catholics Do It With Beads

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Very curious. GetUserName works on Win2K in my apps there were developed on a Win98 box, but doesn't work on a project started on a Win2K box.

    That makes no sense.

  9. #9

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    Can anyone help with integrating the NPGetUser API? I get lost whenever I try to do that on my own. It's description in MSDN looks like it does what I need it to do.

    Thanks

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    A little research turned up this. This does work on Windows 2000, but I do not know about XP
    VB Code:
    1. Private Enum EXTENDED_NAME_FORMAT
    2.     NameUnknown = 0
    3.     NameFullyQualifiedDN = 1
    4.     NameSamCompatible = 2
    5.     NameDisplay = 3
    6.     NameUniqueId = 6
    7.     NameCanonical = 7
    8.     NameUserPrincipal = 8
    9.     NameCanonicalEx = 9
    10.     NameServicePrincipal = 10
    11. End Enum
    12.  
    13. Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    14.  
    15. Private Sub Command1_Click()
    16. Dim sBuffer As String, Ret As Long
    17. sBuffer = String(256, 0)
    18. Ret = Len(sBuffer)
    19. If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
    20.     MsgBox "Username: " + Left$(sBuffer, Ret)
    21. Else
    22.     MsgBox "Error while retrieving the username"
    23. End If
    24. End Sub

  11. #11

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    Worked like a charm! Youdaman.

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Curiosity question: Did you try it with both XP and Win2K?

    I just got a Windows 2000 machine yesterday, but I don't have XP.

    As I said, just curious.

  13. #13

    Thread Starter
    Registered User
    Join Date
    May 2001
    Posts
    61
    Currently, I've only tried it on XP. I have some more scripting to do to before I move it to the 2k box.

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Cool!!

    I tried on my 2000 box and it worked, so if you ran it on XP, then it is gold on both platforms! Thats good to know. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width