Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Retrieving a users login name.


How do I get the user's login name under 32-bit versions of Windows?   

    The following function demonstrates how to use the GetUserName API call to accomplish this task in Delphi 3 or higher:

     
    function GetCurrentUserName: string;
    var
      Len: Cardinal; { This will have to be Integer, not cardinal, in Delphi 3. }
    begin
      Len := 255;  { arbitrary length to allocate for username string, plus one for null terminator }
      SetLength(Result, Len -  ); { set the length }
      if GetUserName(PChar(Result), Len) then  { get the username }
        SetLength(Result, Len -  ) { set the exact length if it succeeded }
      else
      begin
        RaiseLastWin32Error; { raise exception if it failed }
      end;
    end;
    In Delphi 2, use a function like the following:

    function GetCurrentUserName2: string; var Len: integer; buf: array ;0..255] of char; begin Len := SizeOf(buf); if GetUserName(@buf,Len) then begin Result := buf; end else begin Raise Exception.Create('Win32 Error: ' + IntToStr(GetLastError)); { You could use FormatMessage to translate the error code into an error message } end; end;

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use