Web Development

General discussion


Determining the current username

By MaryWeilage Editor ·
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:

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Boring and insufficience article

by nguyen_anh_tuan In reply to Determining the current u ...

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

Collapse -

Current UserName

by NewToTheGame In reply to Determining the current u ...

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

Related Discussions

Related Forums