GetUserNameExA function (secext.h)

Retrieves the name of the user or other security principal associated with the calling thread. You can specify the format of the returned name.

If the thread is impersonating a client, GetUserNameEx returns the name of the client.


  [in]      EXTENDED_NAME_FORMAT NameFormat,
  [out]     LPSTR                lpNameBuffer,
  [in, out] PULONG               nSize


[in] NameFormat

The format of the name. This parameter is a value from the EXTENDED_NAME_FORMAT enumeration type. It cannot be NameUnknown. If the user account is not in a domain, only NameSamCompatible is supported.

[out] lpNameBuffer

A pointer to a buffer that receives the name in the specified format. The buffer must include space for the terminating null character.

[in, out] nSize

On input, this variable specifies the size of the lpNameBuffer buffer, in TCHARs. If the function is successful, the variable receives the number of TCHARs copied to the buffer, not including the terminating null character.

If lpNameBuffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. This parameter receives the required buffer size, in Unicode characters (whether or not Unicode is being used), including the terminating null character.

Return value

If the function succeeds, the return value is a nonzero value.

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

Return code Description
The lpNameBuffer buffer is too small. The lpnSize parameter contains the number of bytes required to receive the name.
The domain controller is not available to perform the lookup
The user name is not available in the specified format.



The secext.h header defines GetUserNameEx as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.


Requirement Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header secext.h (include Security.h)
Library Secur32.lib
DLL Secur32.dll

See also



System Information Functions