Get the network computer and domain names #62

This tip shows how to get the network computer and domain names.

const
  NERR_Success = 0;

function NetWkstaGetInfo(ServerName: LPWSTR; Level: DWORD;
  BufPtr: Pointer): Longint; stdcall;
  external 'netapi32.dll' Name 'NetWkstaGetInfo';

type
  WKSTA_INFO_100 = record
    wki100_platform_id: DWORD;
    wki100_computername: LPWSTR;
    wki100_langroup: LPWSTR;
    wki100_ver_major: DWORD;
    wki100_ver_minor: DWORD;
  end;
  LPWKSTA_INFO_100 = ^WKSTA_INFO_100;

  _USER_INFO_0 = record
    usri0_name: LPWSTR;
  end;

function GetNetParam(AParam: Integer): string;
var
  PBuf: LPWKSTA_INFO_100;
  Res: LongInt;
begin
  Result := '';
  Res := NetWkstaGetInfo(nil, 100, @PBuf);
  if Res = NERR_Success then
  begin
    case AParam of
      0: Result := string(PBuf^.wki100_computername);
      1: Result := string(PBuf^.wki100_langroup);
    end;
 end;
end;

function GetComputerName: string;
begin
  Result := GetNetParam(0);
end;

function GetDomainName: string;
begin
  Result := GetNetParam(1);
end;

Call GetComputerName and GetDomainName to display the required names.

Look up NetWkstaGetInfo in Window help or on MSDN for information on how this works.

Author: Joe Donth
Contributor: Joe Donth
Added: 2008/03/31
Last updated: 2008/03/31