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

后端 未结 6 2079
借酒劲吻你
借酒劲吻你 2020-11-30 13:07

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\"

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 13:36

    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.

提交回复
热议问题