Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting NT User Name from VB

Status
Not open for further replies.

KeithCE

Programmer
Sep 6, 2002
52
US
How can I get the NT User name of someone who is running a VB program. I want to get the name so I can log it.

Thanks for any help.
 
Below is an API call:

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Use the API like this:

Public Function UserName() As String
Dim sBuffer As String
Dim lSize As Long

sBuffer = Space$(255)
lSize = Len(sBuffer)
GetUserName sBuffer, lSize
UserName = Left$(sBuffer, lSize - 1)
End Function
 
The USERNAME environment variable is set by NT for you
 
The USERNAME environment variable is not reliable. For example, the user can change it.
 
strongm,

I am not sure what creates this. You can change it but that change does not seem to persist across process boundaries.
 
strongm,

Ok. Give me a clue. Where do you change it to make it persist.
 
Well, under NT4 you just need to go to Control Panel/System/Environment.
 
Strongm,

On my NT system I can't see the USERNAME environment variable there.
 
Do you guys know how to rename a local user using VB code? I have been trying to find out if it is possible but can't find a definate answer. -----------------------------------------------------
"It's true, its damn true!"
-----------------------------------------------------
 
Yep, that's because you've never set it (so NT provides a default). Just add a USERNAME entry as a User Variable, and it will take precedence. You can quite happily do this for USERDOMAIN, COMPUTERNAME, whatever you like...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top