|
-
Feb 17th, 2006, 06:48 PM
#1
Thread Starter
Lively Member
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!
-
Feb 17th, 2006, 06:57 PM
#2
Re: Help With GetUserName API
You dont need API it can be done like this using an environment variable:
VB Code:
Private Sub Form_Load()
strUser= Environ("USERNAME")
msgbox strUser
End Sub
-
Feb 17th, 2006, 06:58 PM
#3
Thread Starter
Lively Member
Re: Help With GetUserName API
oh, thank you
-
Feb 17th, 2006, 06:58 PM
#4
Hyperactive Member
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:
Dim strUserName As String
Dim lngSize As Long
'Set Max buffer size
lngSize = 100
'Create a buffer
strUserName = String(lngSize, Chr$(0))
'Get the username
GetUserName strUserName, lngSize
'strip the rest of the buffer
strUserName = Left$(strUserName, lngSize - 1)
'Show the username
MsgBox "Hello " + strUserName
To deny our own impulses is to deny the very thing that makes us human
-
Feb 17th, 2006, 07:05 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 17th, 2006, 07:10 PM
#6
Re: Help With GetUserName API
VB Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( _
ByVal lpBuffer As String, _
ByRef nSize As Long _
) As Long
Public Function GetUser() As String
Dim nLen As Long
Dim sBuff As String
'we set sBuff to a size of 25 characters since a user name is probably not
'longer then that. But if it is the function call will fail and we get the required size
'in the nLen argument
nLen = 25
sBuff = Space(nLen)
If GetUserName(sBuff, nLen) Then
'success trim the string and return it
GetUser = Left$(sBuff, nLen - 1)
Else
'the buffer was not big enough change the size and try again
'nLen now contains the size we need to use
sBuff = Space$(nLen)
Call GetUserName(sBuff, nLen)
'Trim of the NULL character
GetUser = Left$(sBuff, nLen - 1)
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|