Share this page 

Get the user name and the computer nameTag(s): WinAPI/Registry


You need to declare two API calls.
FUNCTION long GetComputerNameA &
    (ref string ComputerName, ref ulong BufferLength) 
  LIBRARY "KERNEL32.DLL"
FUNCTION long GetUserNameA(ref string UserName, ref ulong BufferLength) 
LIBRARY "ADVAPI32.DLL"
and then
long ll_ret
string ls_ComputerName, ls_UserName
ulong BufferLength = 250  // you may need to adjust this. see Note

ls_ComputerName = Space(BufferLength)
ls_UserName     = Space(BufferLength)

ll_ret = GetComputerNameA(ls_ComputerName, BufferLength)
ll_ret = GetUserNameA(ls_UserName, BufferLength)

NOTE : From H. Andersson, "In your example to get the username with the function GetUserNameA you take as bufferlength 250. If you have a longer username (for example administrator) the function doesn't return what we expect. I took 100 as bufferlength and now it works."