Solved

Get Domain name

Posted on 2002-05-07
5
1,105 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:mnasman
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now