Title: Read user name from system or network (including Novell)
Question: How to retrieve currently logged-in user name in Delphi from system ? (including WinNT and Novell)
Answer:
There are many ways to do it. Here is a list of methods to read user name. These methods
handle both kinds of names: standalone system (MS Windows) and network user name.
1. Windows API
You call windows API's function GetUserName.
Example:
======================================
function GetSysUserName: string;
const BUF_SIZE = 255;
var dw:DWORD;
Buffer:string;
begin
dw := BUF_SIZE;
SetLength(Buffer,dw);
result :='';
if GetUserName(@(Buffer[1]),dw) then
result :=StrPas(@(Buffer[1]));
end;
======================================
2. BDE
This function works, if BDE is installed. If not, it will not execute correctly, I presume.
Example:
======================================
function GetBDEUserName:string;
var
SysConfigInfo: SYSConfig;
begin
try
Check(DbiGetSysConfig(SysConfigInfo));
result := SysConfigInfo.szUserName;
except
result :=''; // empty name on error
end;
end;
======================================
3. Environment
This method reads environment variable set by Windows system after login in.
Value returned by this function is a user name logged into system (not network).
Example:
======================================
function TForm1.GetEnvirName: string;
var
buf:array[1..512] of char;
iSize:DWORD;
begin
iSize := GetEnvironmentVariable('USERNAME', @buf, 512);
buf[iSize+1] := #0;
result := StrPas(@buf);
end;
======================================
4. MS Access function named "AdvApiGetUserName".
I don't know when this function doesn't work, but it can be usefull if other methods will fail.
Example:
======================================
// import function from DLL
function AdvApiGetUserName(ABuffer:PChar; var nSize:longint):longint; stdcall;
external advapi32 name 'GetUserNameA';
// call function
function TForm1.GetAccessName: string;
var
buf:array[1..512] of char;
iSize:longint;
iError:longint;
begin
iSize := 512;
iError := AdvApiGetUserName(@buf, iSize);
buf[iSize+1] := #0;
result := StrPas(@buf);
end;
======================================
5. Windows network API's function "WNetGetUser".
Another function of Windows API, this time specialized for network environments, but doesn't work
with Novell if double log-in is applied (one log-in into Windows, second into Novell client).
Example:
======================================
function TForm1.GetWNetName: string;
var
buf:array[1..512] of char;
len:DWORD;
begin
len := 512;
if WNetGetUser(nil, @buf, len)NO_ERROR then
result := ''
else begin
result := StrPas(@buf);
end;
end;
======================================
6. Novell registry value
Reads currently logged-in user name from Novell client settings (registry).
Example:
======================================
function TForm1.GetNovRegName: string;
var
Reg :TRegIniFile;
begin
Reg := TRegIniFile.Create('Volatile Environment');
try
Reg.RootKey := HKEY_CURRENT_USER;
result := Reg.ReadString('','NWUSERNAME','');
finally
reg.Free;
end;
end;
======================================
The included Delphi source codes I've developed using Delphi 3. I've also
included a compiled version. Test application is using all methods mentioned above to
read user name and displays results of each method on the screen.