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\"
I have found some places in windows registry with my email or my full name:
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.
Did you try GetUserNameEx(NameDisplay,...)?
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.
NetQueryDisplayInformation should help. The field usri1_full_name will give the full name
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!
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.