Ricerca del nome completo di un utente

I computer possono essere organizzati in un dominio, ovvero una raccolta di computer di rete. L'amministratore del dominio gestisce le informazioni sull'account utente e gruppo centralizzate.

Per trovare il nome completo di un utente, in base al nome utente e al nome di dominio:

  • Convertire il nome utente e il nome di dominio in Unicode, se non sono già stringhe Unicode.
  • Cercare il nome del computer del controller di dominio (DC) chiamando NetGetDCName.
  • Cercare il nome utente nel computer DC chiamando NetUserGetInfo.
  • Convertire il nome utente completo in ANSI, a meno che il programma non funzioni con stringhe Unicode.

Il codice di esempio seguente è una funzione (GetFullName) che accetta un nome utente e un nome di dominio nei primi due argomenti e restituisce il nome completo dell'utente nel terzo argomento.

#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);
}