0

I would like to obtain a unique identifier (GUID not SID) of Windows account which my application is currently running on. I tried with GetUserNameEx with NameUniqueId as format. It works well on some machines but fails with ERROR_NONE_MAPPED on others (which seems to be a known problem):

How do you read the user's display (first and last) name on all versions of Windows reliably?

Getting the full-name of the current user, returns an empty string (C#/C++)

Above mentioned questions concentrate on user name but doesn't answer how to obtain user account GUID.

Community
  • 1
  • 1
tommyk
  • 3,187
  • 7
  • 39
  • 61
  • 1
    If an account does not have a mapped guid, it does not have a guid. Why do you need the guid? – Remy Lebeau May 19 '15 at 18:39
  • @KenWhite indeed you're right. It was my misinterpretation of the documentation plus some misleading information on different forums. Please write this as an answer so that I can close this topic. Thanks. – tommyk May 21 '15 at 07:39

1 Answers1

2

The documentation for GetUserNameEx states that ERROR_NONE_MAPPED means the user name is not available in the specified format. The function cannot return what does not exist; if there is no user account GUID, then the function can't return one.

ERROR_NONE_MAPPED The user name is not available in the specified format.

The same documentation (in the Parameters section, under NameFormat [in]) says (in part):

If the user account is not in a domain, only NameSamCompatible is supported.

So the answer to your question is to try to get the GUID using NameUniqueID, and if that returns ERROR_NONE_MAPPED fall back to using NameSamCompatible, which is available in all cases.

Ken White
  • 123,280
  • 14
  • 225
  • 444