Results 1 to 6 of 6

Thread: Help With GetUserName API

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    114

    Help With GetUserName API

    Well, I'd like to set a variable to the current user's name. I cant quite get a handle on this API because iono what IpBuffer is. Any help is appreciated!

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Help With GetUserName API

    You dont need API it can be done like this using an environment variable:
    VB Code:
    1. Private Sub Form_Load()
    2. strUser= Environ("USERNAME")
    3. msgbox strUser
    4. End Sub
    Chris

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    114

    Re: Help With GetUserName API

    oh, thank you

  4. #4
    Hyperactive Member guyvdn's Avatar
    Join Date
    Oct 2002
    Location
    Belgium
    Posts
    336

    Re: Help With GetUserName API

    · lpBuffer
    Points to the buffer to receive the null-terminated string containing the user’s logon name. If this buffer is not large enough to contain the entire user name, the function fails.

    · nSize
    Pointer to a DWORD that, on input, specifies the maximum size, in characters, of the buffer specified by the lpBuffer parameter. If this buffer is not large enough to contain the entire user name, the function fails. If the function succeeds, it will place the number of characters copied to the buffer into the DWORD that nSize points to.

    VB Code:
    1. Dim strUserName As String
    2.     Dim lngSize As Long
    3.     'Set Max buffer size
    4.     lngSize = 100
    5.     'Create a buffer
    6.     strUserName = String(lngSize, Chr$(0))
    7.     'Get the username
    8.     GetUserName strUserName, lngSize
    9.     'strip the rest of the buffer
    10.     strUserName = Left$(strUserName, lngSize - 1)
    11.     'Show the username
    12.     MsgBox "Hello " + strUserName
    To deny our own impulses is to deny the very thing that makes us human

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help With GetUserName API

    Check out all the different ways in my FAQ thread - http://www.vbforums.com/showthread.php?t=357723
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Help With GetUserName API

    VB Code:
    1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( _
    2.     ByVal lpBuffer As String, _
    3.     ByRef nSize As Long _
    4. ) As Long
    5.  
    6. Public Function GetUser() As String
    7.     Dim nLen As Long
    8.     Dim sBuff As String
    9.     'we set sBuff to a size of 25 characters since a user name is probably not
    10.     'longer then that. But if it is the function call will fail and we get the required size
    11.     'in the nLen argument
    12.     nLen = 25
    13.     sBuff = Space(nLen)
    14.     If GetUserName(sBuff, nLen) Then
    15.         'success trim the string and return it
    16.         GetUser = Left$(sBuff, nLen - 1)
    17.     Else
    18.         'the buffer was not big enough change the size and try again
    19.         'nLen now contains the size we need to use
    20.         sBuff = Space$(nLen)
    21.         Call GetUserName(sBuff, nLen)
    22.         'Trim of the NULL character
    23.         GetUser = Left$(sBuff, nLen - 1)
    24.     End If
    25. End Function

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