Discussion Locked

Determining the current username

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

This week's Visual Basic e-newsletter explains how to determine the name of the current user and then store it in some manner. This tip could come in handy if you'd like to keep track of which users are running a VB6 program.

Will you use these tips to determine the current user? What topics would you like to see us cover in future editions of this e-newsletter? Please let us know.

If you aren't subscribed to our free Visual Basic e-newsletter, click the following link to automatically sign up:
http://nl.com.com/MiniFormHandler?brand=builder&subs_channel=bldr_front_door&list_id=e065&tag=fb

2 replies

by tuanna123

GetUserName API function is very familiar with developers that use API frequently. Why does Builder.com let it bother us?
By the way, the tip itself is insufficience. The second argument of GetUserName API function is the maximum length of username that developer want to get back, if the username is longer than that it (returned string) will be truncated - this is the main different from other APIs, those often return the length of other parameter (returned string) - so we must provide a value for it before calling GetUserName.
Consenquently, the value assigned to second argument depend on developer's purpose. If he want to get the whole name of current user, he must set it to maximum length of username that Windows accept (those of you find it interesting can search MS document). If the developer just want to get first x character of username, he assign x to the second argument.
Peter Aitken's explaination is not clear (and in fact, it does not explain anything).

The sample code is not good, either. It takes time to calculate Len(UserName) and uses a hard code value of 40. I'd rather write it as follow:

'Max length of username we want to get back
Const MaxUsernameLen=40

UserName = Space(MaxUsernameLen)
GetUserName UserName, MaxUsernameLen

by newtothegame

Maybe I am too new to this, however I like to use
Function NameOfUser() As String
NameOfUser = Environ("UserName")
End Function

This discussion is closed to new replies.