사용자의 전체 이름 조회

컴퓨터는 컴퓨터 네트워크의 컬렉션인 도메인으로 구성할 수 있습니다. 도메인 관리자는 중앙 집중식 사용자 및 그룹 계정 정보를 유지 관리합니다.

사용자 이름과 도메인 이름을 지정하여 사용자의 전체 이름을 찾으려면 다음을 수행합니다.

  • 사용자 이름 및 도메인 이름을 유니코드 문자열이 아닌 경우 유니코드로 변환합니다.
  • NetGetDCName을 호출하여 DC(도메인 컨트롤러)의 컴퓨터 이름을 조회합니다.
  • NetUserGetInfo를 호출하여 DC 컴퓨터에서 사용자 이름을 조회합니다.
  • 프로그램에서 유니코드 문자열을 사용할 것으로 예상하지 않는 한 전체 사용자 이름을 ANSI로 변환합니다.

다음 샘플 코드는 처음 두 인수에서 사용자 이름과 도메인 이름을 사용하고 세 번째 인수에서 사용자의 전체 이름을 반환하는 함수(GetFullName)입니다.

#include <windows.h>
#include <lm.h>
#include <stdio.h>

#pragma comment(lib, "netapi32.lib")

BOOL GetFullName( char *UserName, char *Domain, char *dest )
{
    WCHAR wszUserName[UNLEN+1];          // Unicode user name
    WCHAR wszDomain[256]; 
    LPBYTE ComputerName;
    
//    struct _SERVER_INFO_100 *si100;  // Server structure
    struct _USER_INFO_2 *ui;         // User structure

// Convert ANSI user name and domain to Unicode

    MultiByteToWideChar( CP_ACP, 0, UserName,
        (int) strlen(UserName)+1, wszUserName, 
        sizeof(wszUserName)/sizeof(WCHAR) );

    MultiByteToWideChar( CP_ACP, 0, Domain,
        (int) strlen(Domain)+1, wszDomain, 
        sizeof(wszDomain)/sizeof(WCHAR) );

// Get the computer name of a DC for the domain.

    NetGetDCName( NULL, wszDomain, &ComputerName );

// Look up the user on the DC.

    if( NetUserGetInfo( (LPWSTR) ComputerName,
        (LPWSTR) &wszUserName, 2, (LPBYTE *) &ui ) )
    {
        wprintf( L"Error getting user information.\n" );
        return( FALSE );
    }

// Convert the Unicode full name to ANSI.

    WideCharToMultiByte( CP_ACP, 0, ui->usri2_full_name, -1,
        dest, 256, NULL, NULL );

    return (TRUE);
}