Home | Articles | Links | Contact
 
Title Get the user name of the the Windows current session
Description Windows API can provide a lot of information about the system including the name of the user logged in
Date 10/03/2000
Last revision

 

The following function gets the user name from a Windows API function called GetUserName. The only thing we really have to do is to transform the parameters of the API function, which are of types PChar and Cardinal, into the result of a Delphi function with a string type, much easier to use.

function GetUser: string;
var
  name: PChar;
  size: Cardinal;
begin
  size := 255;
  name := StrAlloc( size);
  GetUserName( name, size);
  result := name;
  StrDispose( name)
end;

Every time we use a PChar, we need to allocate space for it with StrAlloc and after using it we have to free the memory occupied by the string with StrDispose.


Was this article useful for you?
Yes No
Any other comments will be welcome

Home | Articles | Links | Contact