Get list of domains on the network

Question :

Get list of domains on the network,

Answer :

Using the Windows API, how can I get a list of domains on my network?


Answered my own question:

Use the function, passing in the SV_TYPE_DOMAIN_ENUM constant for the “servertype” argument.

In Delphi, the code looks like this:

  type    NET_API_STATUS = DWORD;    PSERVER_INFO_100 = ^SERVER_INFO_100;    SERVER_INFO_100 = packed record      sv100_platform_id : DWORD;      sv100_name        : PWideChar;  end;    function NetServerEnum(  //get a list of pcs on the network (same as DOS cmd "net view")    const servername    : PWideChar;    const level         : DWORD;    const bufptr        : Pointer;    const prefmaxlen    : DWORD;    const entriesread   : PDWORD;    const totalentries  : PDWORD;    const servertype    : DWORD;    const domain        : PWideChar;    const resume_handle : PDWORD  ) : NET_API_STATUS; stdcall; external 'netapi32.dll';    function NetApiBufferFree(  //memory mgmt routine    const Buffer : Pointer  ) : NET_API_STATUS; stdcall; external 'netapi32.dll';    const    MAX_PREFERRED_LENGTH = DWORD(-1);    NERR_Success = 0;    SV_TYPE_ALL  = $FFFFFFFF;    SV_TYPE_DOMAIN_ENUM = $80000000;      function TNetwork.ComputersInDomain: TStringList;  var    pBuffer        : PSERVER_INFO_100;    pWork          : PSERVER_INFO_100;    dwEntriesRead  : DWORD;    dwTotalEntries : DWORD;    i              : integer;    dwResult       : NET_API_STATUS;  begin    Result := TStringList.Create;    Result.Clear;      dwResult := NetServerEnum(nil,100,@pBuffer,MAX_PREFERRED_LENGTH,                              @dwEntriesRead,@dwTotalEntries,SV_TYPE_DOMAIN_ENUM,                              PWideChar(FDomainName),nil);      if dwResult = NERR_SUCCESS then begin      try        pWork := pBuffer;        for i := 1 to dwEntriesRead do begin          Result.Add(pWork.sv100_name);          inc(pWork);        end;  //for i      finally        NetApiBufferFree(pBuffer);      end;  //try-finally    end  //if no error    else begin      raise Exception.Create('Error while retrieving computer list from domain ' +                             FDomainName + #13#10 +                             SysErrorMessage(dwResult));    end;  end;    

That’s the answer Get list of domains on the network, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Import Namespace System.Query