Главная страница | назад





Article #20178: How to access the current user's login ID.

Question:

How do I get the username for the current user?

Answer:

Use the windows API function GetUserName. From the win32 API help:

BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);

Parameters:

lpBuffer
Pointer to the buffer to receive the null-terminated string containing 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.
nSize
Pointer to a DWORD variable that, on input, specifies the maximum size, in characters, of the buffer specified by the lpBuffer parameter. If the function succeeds, the variable receives the number of characters copied to the buffer. If the buffer is not large enough, the function fails and the variable receives the required buffer size, in characters, including the terminating null character.

Last Modified: 11-JAN-00