//-------------------------------------------------------------------
//-- Looking up User Name C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//--       you must not claim that you wrote the original code.
//--       If you use this code, an acknowledgment in the
//--       documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

void* getUserNamefromRegistry( TCHAR* queryString );

//===================================================================
// check the two normal places in the registry
// HKEY_LOCAL_MACHINE
//         SOFTWARE
//                 Microsoft
//                         Windows NT
//                                 CurrentVersion
//                                         Winlogon
//
//                                         AltDefaultUserName
//
//                                         DefaultUserName
//
//===================================================================
int main( int argc, char* argv[] )
{

     // see who is logged on per the registry HKLM
     TCHAR* userName = (TCHAR*)getUserNamefromRegistry( _T("AltDefaultUserName") );
     if ( userName != NULL )
     {
            printf( "\nUser name:     %s", userName );

            free( userName );
            userName = NULL;
     }

     // see who is logged on per the registry HKLM
     userName = (TCHAR*)getUserNamefromRegistry( _T("DefaultUserName") );
     if ( userName != NULL )
     {
            printf( "\nUser name:     %s", userName );

            free( userName );
            userName = NULL;
     }

}


//===================================================================
// get the user name from the registry
// need to check to see if the function returns NULL or some data
// also the calling function has to free up the returned data
//===================================================================
void* getUserNamefromRegistry( TCHAR* queryString )
{
     void* userData = NULL;
     HKEY hKey = NULL;
     DWORD valType = 0;
     DWORD valSize = 0;
     long errCode = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                                _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"),
                                                           0,
                                                           KEY_READ,
                                                           &hKey );
     if ( errCode == ERROR_SUCCESS )
     {
            // get the size of the memory we need to allocate
            errCode = RegQueryValueEx( hKey,
                                                             queryString,
                                                             0,
                                                             &valType,
                                                             NULL,
                                                             &valSize );

            if ( errCode == ERROR_SUCCESS )
            {
                 // allocate memory for the call
                 userData = malloc( valSize + 2 );
                 if ( userData != NULL )
                 {
                        errCode = RegQueryValueEx( hKey,
                                                                         queryString,
                                                                         0,
                                                                         &valType,
                                                                         (BYTE*)userData,
                                                                         &valSize );

                 }
            }

            RegCloseKey( hKey );
     }

     return userData;

}



Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.