Termopetteri
Termopetteri

Reputation: 65

How to get logged-in user's full name in windows?

How to get logged-in user's full name (the one he/she entered as his/her real name) using windows API or something else? For example how to get "John Smith", not "john" (as it were his username).

GetUserName(...) doesn't do the job because it returns the username, not the full name.

Upvotes: 6

Views: 20203

Answers (6)

user2482613
user2482613

Reputation: 87

NetQueryDisplayInformation should help. The field usri1_full_name will give the full name

Upvotes: 0

Warren  P
Warren P

Reputation: 68862

I believe I have found a related answer which works better than GetUserNameEx alone, that is to say, I can handle some cases where GetUserNameEx fails.

My alternative answer is here including sample code for Delphi.

In short, if GetUserInfoEx(3,...) fails, read GetUserInfoEx(2,...) which returns a name in the form "machinename\username", which you can then pass into NetUserInfo functions in NETAPI32.dll, which will read the local SAM database, which is where the user's full name is stored, if they have set it in the local SAM database. Of course, many home non-domain users have never set this up, so the other answers here are likely to provide some hints too.

Upvotes: 0

Anders
Anders

Reputation: 101579

Did you try GetUserNameEx(NameDisplay,...)?

Upvotes: 4

Termopetteri
Termopetteri

Reputation: 65

I have found some places in windows registry with my email or my full name:

  1. HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo has UserName which contains my full name.
  2. KEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer has Logon User Name which contains my email.
  3. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon has DefaultUserName which contains my email.

The first one obviously requires Microsoft Office to be installed and that the user has entered his/her name in the Office settings. The second and the third are more reliable but contains email instead but email is also good (or better) identification than full name.

Upvotes: 0

Roman Starkov
Roman Starkov

Reputation: 61382

A quick Google reveals that NetUserGetInfo should do this. It doesn't look like the easiest API in the world to use.

I think the level you're after is 10, which returns a USER_INFO_10 structure, containing, among other things, a usri10_full_name.

Make sure you remember to free the structure when finished, using NetApiBufferFree!

Upvotes: 3

EboMike
EboMike

Reputation: 77722

Well, if the user never entered it, there's no way for you to get it. You could look for installed email programs and politely ask them for the info, but that's a bad idea for many reasons.

Here's your best shot: Get the name the user entered when registering the copy of Windows. This is in the registry. The exact location differs between Windows versions, but in recent versions, it's at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion (and in Windows 95/98/ME, at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion).

In there, you'll find RegisteredOwner, which is where the was supposed to enter the name. Obviously, if this is a company computer and the machine was set up by IT, it's very common to find a standardized company string there. And, of course, lots of people will enter names there like "Joe Sixpack" or "Cracked by Quartex". However, that's as close as you can get.

Upvotes: 0

Related Questions