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!
Printable View
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!
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
oh, thank you :)
· 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
Check out all the different ways in my FAQ thread - http://www.vbforums.com/showthread.php?t=357723
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