C++ – Getting logged on user’s name with or without domain in Windows

mfcvisual c++winapi

Is there a direct API to get the currently logged in user's name with the domain? So, it would return something like "domain\user" when a machine is on the domain, but at the same time it would return "user" when the machine is not on the domain? If there's not, what's the best way to get this information?

I noticed there's a LookupAccountName function – would that be the right direction to take?

Best Answer

Try GetUserNameEx(). It supports various name formats.