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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows logon name?

Status
Not open for further replies.

inetd

Technical User
Jan 23, 2002
115
0
0
HK
How can I get the current logon user name(including the domain name)? Any API?

e.g. mydomain\myusername

Thanks.
 
You may use environment variables "USERNAME" and "USERDOMAIN".
 
Thanks rpet.
Can you tell in more detail? How should I use these environment variables?

Thanks again.
 
Read their values via API function
Code:
GetEnvironmentVariable()
.
 
I had a problem using the environment variables some years ago.
On a company network the variables were cleared for some reason, probably by the login script.

I had to use the GetUserName API to get the user name.
 
How can I get the current logon user name(including the domain name)? Any API?


Create a new form with one button and one label. Do not rename either.

In the 'on-click' event for the button type the following code so the whole event looks like this.

//----------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD dwSize = MAX_PATH+1;
char szBuf[MAX_PATH+1];
szBuf[0] = '\0';
GetUserName(szBuf, &dwSize);
Label1->Caption = szBuf;
}
//----------------------------------------------------------

When you click the button, the PC's username appears in the label. Viola!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top