Q:  How do I get the network user name without using the BDE?

A:

This way uses the 32 bit API:

function GetNetUserName: string;
const
  NetUserNameLength: integer = 50;
begin
  SetLength(result, NetUserNameLength);
  GetUserName(pChar(result), NetUserNameLength);
  SetLength(result, StrLen(pChar(result)));
end;
 

This way uses the registry:  It reads the key groups under the network\persistent section into a TStringList.  Then, we traverse the list until we find a UserName key and grab that value.  If we don't find that name, we keep looking until we run out of entries in that list.  If nothing is found, an empty string is returned.

function GetNetUserName: string;
var
  reg: TRegIniFile;
  RegKeys: TStringList;
  s: string;
  i: integer;
begin
  reg := TRegIniFile.create('network\persistent');
  try
    RegKeys := TStringList.create;
    try
      reg.ReadSections(RegKeys);
      i := 0;
      repeat  // Make sure that there is a UserName key.
        s := reg.ReadString(RegKeys[i], 'UserName', '');
        inc(i);
      until (s <> '') or (i = RegKeys.count);
      result := s;
    finally
      RegKeys.free;
    end;
  finally
    reg.free;
  end;
end;