한 걸음씩..

[Windows] 현재 로그인한 유저 이름 알아오기 본문

프로그래밍

[Windows] 현재 로그인한 유저 이름 알아오기

smdy0426 2013. 9. 6. 09:30
반응형


Windows 환경에서 로그인 중인 유저 이름을 알아올 수 있다.


       WinAPI 사용하는 방법


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.




      환경변수를 사용하는 방법


LPTSTR UserName;

size_t requiredSize;

_tgetenv_s( &requiredSize, NULL, 0, _T("USERNAME") );

UserName = (LPTSTR) malloc( requiredSize );

if ( UserName != NULL )

{

_tgetenv_s( &requiredSize, UserName, requiredSize, _T("USERNAME") );

}


반응형