Click to Rate and Give Feedback
MSDN
MSDN Library
Windows Development
System Services
System Information
 GetUserNameEx function
GetUserNameEx function

Applies to: desktop apps only

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.

Syntax

BOOLEAN WINAPI GetUserNameEx(
  __in     EXTENDED_NAME_FORMAT NameFormat,
  __out    LPTSTR lpNameBuffer,
  __inout  PULONG lpnSize
);

Parameters

NameFormat [in]

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.

lpNameBuffer [out]

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

lpnSize [in, out]

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 codeDescription
ERROR_MORE_DATA

The lpNameBuffer buffer is too small. The lpnSize parameter contains the number of bytes required to receive the name.

ERROR_NO_SUCH_DOMAIN

The domain controller is not available to perform the lookup

ERROR_NONE_MAPPED

The user name is not available in the specified format.

 

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Secext.h (include Security.h)

Library

Secur32.lib

DLL

Secur32.dll

Unicode and ANSI names

GetUserNameExW (Unicode) and GetUserNameExA (ANSI)

See also

EXTENDED_NAME_FORMAT
LookupAccountName
System Information Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
how to get this to work      Jim Michaels   |   Edit   |   Show History
the only thing that worked for me to get rid of the "define one of these" errors was to
//#define SECURITY_KERNEL
#define SECURITY_WIN32
//#define SECURITY_MAC
#include <security.h>
#include <secext.h>
GetUserNameEx()

(note the order)

in an earlier post, I had incorrectly stated that I had thought that only SECURITY_KERNEL worked. 

if you forget to include security.h, then only SECURITY_KERNEL works, but if I include security.h before secext.h I was able to #define either SECURITY_WIN32 or SECURITY_KERNEL. (I did not test SECURITY_MAC because I don't have a mac - what does SECURITY_MAC mean anyway?  what does SECURITY_KERNEL mean?  what are these and what does it do to my code?)

I also incorrectly stated that you only had to include the 2 headers to get this working (this is what the manual currently says).
After some testing I discovered this also to be false.  you absolutely MUST #define ONE of the SECURITY_ codes listed above at the top of your program or it will compile with an error stating that you must #define one of the 3 SECURITY_ codes above.

Tags What's this?: Add a tag
Flag as ContentBug
Intellisense errors      sixstorm1   |   Edit   |   Show History
When I include security.h in my project, I get 23 errors in sspi.h, including "identifier "PUNICODE_STRING" is undefined", "#error directive: You must define one of SECURITY_WIN32, SECURITY_KERNEL, or" and "#error directive: SECURITY_MAC".

What's wrong? I am using VS 2010 Beta 2 and Windows 7 64-bit.

I guess I gotta replace PUNICODE_STRING by LPCWSTR, since I think it's equivalent.

This isn't documented anywhere.

Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker