简体   繁体   中英

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

  • I try to get the full-name of the current log-in user (Fullname, not username).

  • The following code C#, C++ works fine but on XP computers not connected to the Net, I get empty string as result if I run it ~20 minutes after login (It runs OK whithin the first ~20 minutes after login)

  • A Win32 API (GetUserNameEx) is used rather that PrincipalContext since it PrincipalContext may takes up to 15 seconds when working offline.

  • Any Help why am I getting an empty string as result though a user full name 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;
}

The function GetUserNameEx with NameDisplay can't work in offline mode . This information is only accessible when the computer is online. I recommend you to implement some caching of information like full name or other which is accessible in online mode only. For example, if the computer is online you can retrieve and save information like Full User Name. So you can have in some your config-file of in registry a mapping between users SID and it's full name. If you don't able give full name directly you can get the information from your cash.

Windows has a lot of different notification (like NotifyAddrChange ) which you can use (if needed) to monitor change from online to offline mode and back.

Most information which you can get about current user session (also in offline mode) you can get from LsaGetLogonSessionData and WTSQuerySessionInformation API ( GetUserNameEx you already know), but you will not find full user name inside.

If you do find a way to get full name of user in offline mode please post the information to me.

Try to use GetUserNameExA (for ASCII) instead of GetUserNameEx macro. Does it help? Print also the output of the program.

I'm curious: for a 'permamently offline' station, where (in the OS) is the user name stored? By browsing control panel Users, it looks like local user accounts have no place to store a 'NameDisplay', there's only a user name.

Where that data is stored for a non-connected node would be a mystery to me. If (in fact) the data is only stored with the domain controller, the only thing I can think of is to cache the information as mentioned earlier.

So use the 1st one, check result and then invoke the 2nd via async delegate. Your app won't get any lag and full name is certainly not it's core feature - I hope :-)

Please try disabling the UAC on your machine. I guess it should work after that.

Steps to disable it.

  1. Go to control panel.
  2. Then to Users.
  3. Select which admin account you wanted to retain.
  4. Delete the account with admin privilege, which you do not wish to have.

If in future you want to create any new account with admin rights, you create an account and make it as an admin account using change account type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM