Solved

Get Domain name

Posted on 2002-05-07
5
1,107 Views
Last Modified: 2011-09-20
I'm looking for a piece of code to supply me with the Domain name, which the user is logged on to.
I already have the userid.
This should work for both WinNT and Win2K !
0
Comment
Question by:DelRunner
5 Comments
 
LVL 2

Accepted Solution

by:
Tasomia earned 150 total points
ID: 6993779
Hi,

Try this code:

function GetDomainName: AnsiString;
type
  WKSTA_INFO_100 = record
    wki100_platform_id: Integer;
    wki100_computername: PWideChar;
    wki100_langroup: PWideChar;
    wki100_ver_major: Integer;
    wki100_ver_minor: Integer;
  end;

  WKSTA_USER_INFO_1 = record
    wkui1_username: PChar;
    wkui1_logon_domain: PChar;
    wkui1_logon_server: PChar;
    wkui1_oth_domains: PChar;
  end;
type
  //Win9X ANSI prototypes from RADMIN32.DLL and RLOCAL32.DLL

  TWin95_NetUserGetInfo = function(ServerName, UserName: PChar; Level: DWORD; var
    BfrPtr: Pointer): Integer;
  stdcall;
  TWin95_NetApiBufferFree = function(BufPtr: Pointer): Integer;
  stdcall;
  TWin95_NetWkstaUserGetInfo = function(Reserved: PChar; Level: Integer; var
    BufPtr: Pointer): Integer;
  stdcall;

  //WinNT UNICODE equivalents from NETAPI32.DLL

  TWinNT_NetWkstaGetInfo = function(ServerName: PWideChar; level: Integer; var
    BufPtr: Pointer): Integer;
  stdcall;
  TWinNT_NetApiBufferFree = function(BufPtr: Pointer): Integer;
  stdcall;

  function IsWinNT: Boolean;
  var
    VersionInfo: TOSVersionInfo;
  begin
    VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
    Result := GetVersionEx(VersionInfo);
    if Result then
      Result := VersionInfo.dwPlatformID = VER_PLATFORM_WIN32_NT;
  end;
var

  Win95_NetUserGetInfo: TWin95_NetUserGetInfo;
  Win95_NetWkstaUserGetInfo: TWin95_NetWkstaUserGetInfo;
  Win95_NetApiBufferFree: TWin95_NetApiBufferFree;

  WinNT_NetWkstaGetInfo: TWinNT_NetWkstaGetInfo;
  WinNT_NetApiBufferFree: TWinNT_NetApiBufferFree;

  WSNT: ^WKSTA_INFO_100;
  WS95: ^WKSTA_USER_INFO_1;

  EC: DWORD;
  hNETAPI: THandle;
begin
  try

    Result := '';

    if IsWinNT then
    begin
      hNETAPI := LoadLibrary('NETAPI32.DLL');
      if hNETAPI <> 0 then
      begin @WinNT_NetWkstaGetInfo := GetProcAddress(hNETAPI, 'NetWkstaGetInfo');
          @WinNT_NetApiBufferFree  := GetProcAddress(hNETAPI, 'NetApiBufferFree');

        EC := WinNT_NetWkstaGetInfo(nil, 100, Pointer(WSNT));
        if EC = 0 then
        begin
          Result := WideCharToString(WSNT^.wki100_langroup);
          WinNT_NetApiBufferFree(Pointer(WSNT));
        end;
      end;
    end
    else
    begin
      hNETAPI := LoadLibrary('RADMIN32.DLL');
      if hNETAPI <> 0 then
      begin @Win95_NetApiBufferFree := GetProcAddress(hNETAPI, 'NetApiBufferFree');
          @Win95_NetUserGetInfo := GetProcAddress(hNETAPI, 'NetUserGetInfoA');

        EC := Win95_NetWkstaUserGetInfo(nil, 1, Pointer(WS95));
        if EC = 0 then
        begin
          Result := WS95^.wkui1_logon_domain;
          Win95_NetApiBufferFree(Pointer(WS95));
        end;
      end;
    end;

  finally
    if hNETAPI <> 0 then
      FreeLibrary(hNETAPI);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetDomainName);
end;
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6995862
Hello

  you can get it easily from the Windows registry

uses Registry;

procedure TForm1.Button1Click(Sender: TObject);
 begin
    with TRegistry.Create do
    begin
      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey( 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon', False );
      ShowMessage(ReadString( 'DefaultDomainName' ));
    end;
 end;

Best regards
Mohammed Nasman
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6995872
mnasman : is that key present in all windows versions ?
0
 
LVL 2

Expert Comment

by:cqhall
ID: 7000367
We use this approach in Win95 / NT4 / W2K...

Function GetEnvStr(EnvName: AnsiString): AnsiString;
var
 Buffer: Array[1..256] of Char;
begin
 EnvName:='%'+EnvName+'%'+#0;
 ExpandEnvironmentStrings(@EnvName[1],@Buffer,SizeOf(Buffer));
 Result:=StrPas(@Buffer);
 if (Result+#0=EnvName) then Result:='';
end;

Function GetDomain: AnsiString;
Const
 Domain : AnsiString='';
var
 RegData: TRegistry;
begin
 if (Domain='') then
 begin
  Domain:=SysUtils.UpperCase(GetEnvStr('USERDOMAIN'));
  if (Domain='') then
  begin
   RegData:=TRegistry.Create;
   With RegData do
   begin
    RootKey:=HKEY_LOCAL_MACHINE;
    if OpenKey('\System\CurrentControlSet\Services\MSNP32\NetworkProvider',False) then
    begin
     Domain:=SysUtils.UpperCase(ReadString('AuthenticatingAgent'));
    end else
    begin
     if OpenKey('\System\CurrentControlSet\Services\VXD\VNETSUP',False) then
     begin
      Domain:=SysUtils.UpperCase(ReadString('Workgroup'));
     end;
    end;
    Free;
   end;
  end;
 end;
 Result:=Domain;
end;
0
 
LVL 1

Author Comment

by:DelRunner
ID: 7000658
Worked OK. I've had problems with other components/code, which didn't work unless you had admin rights.

Good job !
DR
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question