Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetUserName

Status
Not open for further replies.

sifitz

Programmer
Feb 27, 2002
47
0
0
GB
I have been using the GetUserName function to find the windows user. But I would like to find out the real name of the user. How would I find this?

I am using VB6.

Thanks,

Si Fitz
 
Once you have the logged on user name, you can use the NetGerUserInfo function to get the full name from the NT domain controller. You need to use the level 2 structure to fetch this information.

The following code sample is from MSDN:

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

BOOL GetFullName( char *UserName, char *Domain, char *dest )
{
WCHAR wszUserName[256]; // 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,
strlen(UserName)+1, wszUserName,
sizeof(wszUserName)/sizeof(wszUserName[0]) );
MultiByteTOWideChar( CP_ACP, 0, Domain,
strlen(Domain)+1, wszDomain, sizeof(wszDomain)/sizeof(wszDomain[0]) );

// 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 ) )
{
printf( &quot;Error getting user information.\n&quot; );
return( FALSE );
}

// Convert the Unicode full name to ANSI.

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

return (TRUE);
}


Alternatively, you can use ADSI. It can be easier to use. I will leave that as a subject for a follow up question if you want to pursue that option.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top