Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
[Full disclosure: I've posted a similar question (same subject line) on "another developer forum" and received an answer there that isn't wholly satisfactory. I'm slightly rephrasing the question here in the hopes of maybe getting something different.]

I'm looking for the C++/WinRT UWP equivalent of the non-UWP Windows functions to get the login ID and display name for the owner of the current process:

GetUserNameW (_, _);
GetUserNameExW (_, _, _);


What I have tried:

In C++/WinRT I've found these two functions, but there's no apparent way to get from the IBuffer sysId for the current user returned by the first function, to the hstring userId required by the second. The properties I'm after require a User object, from an hstring userId:

#include <winrt/Windows.System.h>
#include <winrt/Windows.System.Profile.h>
using namespace winrt;
using namespace winrt::Windows;

IBuffer sysId = // "pass null into this ... which gets the current user's ID."
    System::Profile::SystemIdentification::GetSystemIdForUser(nullptr).Id();
hstring userId;  // = ?  Can't get here from there
System::User user = System::User::GetFromId (userId);
// Once I have the User object, I can get the properties I seek


Seemed simple enough at first, but there's evidently no way to get the required userId from the sysId. (No way documented or suggested so far. I've tried various strategies, but none have worked.)

Instead, I was advised to use this function to get a list of users. The default (with no arguments) retrieves the entire list. The arguments restrict the results to LocalUser, LocallyAuthenticated, and the user I seek should be returned in vector element 0:

Windows::Foundation::Collections::IVectorView<Windows::System::User> users =
    co_await Windows::System::User::FindAllAsync (
        Windows::System::UserType::LocalUser,
        Windows::System::UserAuthenticationStatus::LocallyAuthenticated);


I have a few problems with this:

* On the HoloLens 2, authenticated through a Microsoft account, that function returned 0 users. I had to omit not only the LocallyAuthenticated, but also (surprisingly) the LocalUser restriction to get a non-empty result.

* On a desktop system, through "Switch user", there may be multiple simultaneous LocallyAuthenticated LocalUsers. Being "socially distanced" and working from home, I can't invite another user to log into my machine locally to test under that condition. When I raised this issue on "the other forum", the responder said he tried it and "it just returned the current user instead of all of them." Probably OK, but that means that in the unlikely event that my app is left running in the background it could get the wrong answer. If it querys this only at program initialization, there's a tiny but non-zero chance of getting the wrong answer.

* It still means I need different code for a desktop app (specifying LocalUser, LocallyAuthenticated) versus a HoloLens app (without those specifications). Not a huge problem, because the C++/WinRT UWP variant is mostly targeting the HoloLens 2; we already have a non-UWP variant that works happily on the desktop. But still, it would be preferable to have UWP code that runs on the desktop also, at least for some development and testing.

Another strategy occurred to me: I could iterate through the list of users returned by FindAllAsync(_, _), get the sysId for each, and compare with the sysId for the current user (obtained by passing nullptr to GetSystemIdForUser(_).Id()). I tried this, but none of the user system IDs compared equal to the ID obtained with nullptr.

C++/WinRT UWP seems messier and more arcane than it ought to be. I just want to identify the user who's running the program. The program has a collaboration aspect; it's nice to tag collaborators with some kind of identity.
Posted
Updated 24-Jul-20 12:10pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900