svět, kde se rodí nová budoucnost   
 
Delphi headDelphi Tips

Get the username and computer name from a computer

For Win95, you can find it in the 
registry - but this is not portable 
to NT. A safer way and one which is 
portable to NT) would be to use the 
Win32 calls GetComputerName and 
GetUserName, both of which are 
defined in the Windows unit.
Each of these functions takes a buffer as its first 
parameter and the length of the buffer as its second.
The function definitions are shown below:

function GetComputerName(lpBuffer: PChar; 
  var nSize: DWORD): BOOL; stdcall;
function GetUserName(lpBuffer: PChar; 
  var nSize: DWORD): BOOL; stdcall;


Use them like this:


 function  GetWindowsUserName : string;
const
  cnMaxLen=254;
var
  sUserName     : string;
  dwUserNameLen : DWord;
begin
  dwUserNameLen:=cnMaxLen-1;
  SetLength(sUserName, cnMaxLen);
  GetUserName(Pchar(sUserName), dwUserNameLen);
  SetLength(sUserName, dwUserNameLen);
  result:=sUserName;
  if dwUserNameLen=cnMaxLen-1 then
    result:='';
end;

Back to Index of Tips

 

 

Send mail to radek.novak@infojet.cz with questions or comments about this web site.
Copyright © 1999-2002 Infojet.cz
Last modified: 26.07.2002