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

  • I am trying to get the full name of the current login user (full name, not username).

  • The following C #, C ++ code works fine, but on XP computers that are not connected to the network, I get an empty string as a result if I run it 20 minutes after logging in (it works fine in the first 20 minutes after Login )

  • The Win32 API (GetUserNameEx) is used rather than the PrincipalContext, since it can take up to 15 seconds for a PrincipalContext when working offline.

  • Any help Why am I getting an empty string as a result if the full username is specified ???

- C # code

public static string CurrentUserFullName { get { const int EXTENDED_NAME_FORMAT_NAME_DISPLAY = 3; StringBuilder userName = new StringBuilder(256); uint length = (uint) userName.Capacity; string ret; if (GetUserNameEx(EXTENDED_NAME_FORMAT_NAME_DISPLAY, userName, ref length)) { ret = userName.ToString(); } else { int errorCode = Marshal.GetLastWin32Error(); throw new Win32Exception("GetUserNameEx Failed. Error code - " + errorCode); } return ret; } } [DllImport("Secur32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool GetUserNameEx(int nameFormat, StringBuilder lpNameBuffer, ref uint lpnSize); 

- code in C ++

 #include "stdafx.h" #include <windows.h> #define SECURITY_WIN32 #include <Security.h> #pragma comment( lib, "Secur32.lib" ) int _tmain(int argc, _TCHAR* argv[]) { char szName[100]; ULONG nChars = sizeof( szName ); if ( GetUserNameEx( NameDisplay, szName, &nChars ) ) { printf( "Name: %s\n", szName); } else { printf( "Failed to GetUserNameEx\n" ); printf( "%d\n", GetLastError() ); } return 0; } 
+4
source share
5 answers

The GetUserNameEx function with NameDisplay cannot work offline . This information is only available if the computer is online. I recommend that you implement some caching information, such as a full name or another, available only in online mode. For example, if the computer is online, you can retrieve and save information, such as Full username. Thus, you can have a mapping between the SID of users and its full name in your registry configuration file. If you cannot give the full name directly, you can get information from your money.

There are many different notifications on Windows (e.g. NotifyAddrChange ) that you can use (if necessary) to monitor changes online and offline.

Most of the information you can get about the current user session (also offline) can be obtained from the LsaGetLogonSessionData and WTSQuerySessionInformation API ( GetUserNameEx , which you already know), but you will not find the full username inside.

If you find a way to get your full username offline, write me this information.

+1
source

Try using GetUserNameExA (for ASCII) instead of the GetUserNameEx macro. Does it help? Print the program output as well.

0
source

I'm curious: for the "permamently offline" station, where (in the OS) is the username stored? When viewing the control panel users, it seems that the local user accounts do not have a place to store "NameDisplay", there is only the username.

If this data is stored for an unrelated node, this will be a mystery to me. If (in fact) the data is stored only with a domain controller, the only thing I can think of is to cache the information, as mentioned earlier.

0
source

So, use 1st, check the result, and then call 2nd through the async delegate. Your application will not get any delay, and the full name, of course, is not its main feature - I hope :-)

0
source

Try disabling UAC on your computer. I think this should work after that.

Steps to turn it off.

  • Go to the control panel.
  • Then for users.
  • Select which administrator account you want to keep.
  • Delete an account with administrator rights that you do not want to have.

If in the future you want to create a new account with administrator rights, you will create an account and make it an administrator account using the change account type.

-1
source

Source: https://habr.com/ru/post/1312085/


All Articles