Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Domain name

Posted on 2002-05-07
5
Medium Priority
?
1,114 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 600 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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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