Solved

Get Domain name

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

23 Experts available now in Live!

Get 1:1 Help Now