Solved

Get Domain name

Posted on 2002-05-07
5
1,113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month5 days, 3 hours left to enroll

636 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