TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Determining the Windows User Name
 
   
 Posted: 26 September 2000
 
   
 
 Applies to: Delphi 4.0
 
   
 
Audience: Intermediate
 
       
   

Question: How can I determine the name of the user currently using Windows?

Answer: The getUserName() API function provides the name used to log into Windows. Here's one way to call it:

function getUserLogin : String;
var
   pcUserName : pChar; // temporary storage
   dwMaxChars : dWord; // length parameter
const
   MAXCHARS = 255;  // max length
begin

   dwMaxChars := MAXCHARS;
   pcUserName := strAlloc( MAXCHARS + 1 );
   try
      if not getUserName( pcUserName, dwMaxChars ) then
         result := ''
      else
         result := string( pcUserName );
   finally
      strDispose( pcUserName );
   end;

end;

Please note that this returns the user name entered into the Windows login dialog, which may not necessarily be the same as the user's network user name.

Also, note that this function returns an empty string if getUserName() fails, for whatever reason.

For more information, see the online Help entry for getUserName()

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]