GetUserName Function

Retrieves the name of the user associated with the current thread.

Use the GetUserNameEx function to retrieve the user name in a specified format. Additional information is provided by the IADsADSystemInfo interface.

Syntax

C++
BOOL WINAPI GetUserName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);

Parameters

lpBuffer [out]

A pointer to the buffer to receive the user's logon name. If this buffer is not large enough to contain the entire user name, the function fails. A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.

lpnSize [in, out]

On input, this variable specifies the size of the lpBuffer buffer, in TCHARs. On output, the variable receives the number of TCHARs copied to the buffer, including the terminating null character.

If lpBuffer is too small, the function fails and GetLastError returns ERROR_INSUFFICIENT_BUFFER. This parameter receives the required buffer size, including the terminating null character.

If this parameter is greater than 32767, the function fails and GetLastError returns ERROR_INSUFFICIENT_BUFFER.

Return Value

If the function succeeds, the return value is a nonzero value, and the variable pointed to by lpnSize contains the number of TCHARs copied to the buffer specified by lpBuffer, including the terminating null character.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.

Examples

For an example, see Getting System Information.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Advapi32.lib

DLL

Advapi32.dll

Unicode and ANSI names

GetUserNameW (Unicode) and GetUserNameA (ANSI)

See Also

GetUserNameEx
LookupAccountName
System Information Functions

 

 

Send comments about this topic to Microsoft

Build date: 2/4/2010

Tags :


Community Content

Matthew J
Case matters
It is worth noting that the case of the returned value is set only by the text typed by the user. Thus if they log in using "Matthew", that's what you get. If they use "matthew", that is also what you get. So if you are using this for lookups, you should use a case-insensitive comparison. This bit me!
Tags : contentbug

dmex
vb.net syntax
<DllImport("advapi32.dll", CharSet:=CharSet.Auto)> Public Shared Function GetUserName(ByVal lpBuffer As StringBuilder, ByVal nSize As Integer()) As Boolean
End Function
Tags :

dmex
C# syntax
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern bool GetUserName(StringBuilder lpBuffer, int[] nSize);
Tags :

SOLISYS
GetUserName() - Not returning the "User" name
Hi, I'been trying to get the actual user name, hence the use of this function, but somehow it's not working for me. I'm running Vista btw, here is an example:

// VISTAVIRTUAL is the name of my PC

GetUserNameExW(NameSamCompatible, username, &l); //return "WORKGROUP\VISTAVIRTUAL$"

GetUserNameExW(NameDnsDomain, username, &l); //returns "\VISTAVIRTUAL$"

GetUserNameW(NameDnsDomain, username, &l); //returns "SYSTEM"

Any clues as to why I'm not getting the actual user name? My buffer space is sufficient for a 255 output.

Thanks!

Tags :

gavery
You cannot rely on system context returning "SYSTEM"
Dutch Vista returns "SYSTEEM", but Dutch XP and every other language I've seen (approx 10+) return "SYSTEM", so checking for system account by comparing username with "SYSTEM" does not work in all cases :-(
Tags :

Page view tracker