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

[{'answer_id': 8881, 'author': 'JosephStyons', 'author_id': 672, 'author_profile': 'https://Stackoverflow.com/users/672', 'pm_score': 2, 'selected': False, 'text': '<p>Answered my own question:</p>\n\n<p>Use the <a href="http://msdn.microsoft.com/en-us/library/aa370623(VS.85).aspx" rel="nofollow noreferrer">NetServerEnum</a> function, passing in the <code>SV_TYPE_DOMAIN_ENUM</code> constant for the "servertype" argument.</p>\n\n<p>In Delphi, the code looks like this:</p>\n\n<pre><code>&lt;snip&gt;\ntype\n  NET_API_STATUS = DWORD;\n  PSERVER_INFO_100 = ^SERVER_INFO_100;\n  SERVER_INFO_100 = packed record\n    sv100_platform_id : DWORD;\n    sv100_name        : PWideChar;\nend;\n\nfunction NetServerEnum(  //get a list of pcs on the network (same as DOS cmd "net view")\n  const servername    : PWideChar;\n  const level         : DWORD;\n  const bufptr        : Pointer;\n  const prefmaxlen    : DWORD;\n  const entriesread   : PDWORD;\n  const totalentries  : PDWORD;\n  const servertype    : DWORD;\n  const domain        : PWideChar;\n  const resume_handle : PDWORD\n) : NET_API_STATUS; stdcall; external \'netapi32.dll\';\n\nfunction NetApiBufferFree(  //memory mgmt routine\n  const Buffer : Pointer\n) : NET_API_STATUS; stdcall; external \'netapi32.dll\';\n\nconst\n  MAX_PREFERRED_LENGTH = DWORD(-1);\n  NERR_Success = 0;\n  SV_TYPE_ALL  = $FFFFFFFF;\n  SV_TYPE_DOMAIN_ENUM = $80000000;\n\n\nfunction TNetwork.ComputersInDomain: TStringList;\nvar\n  pBuffer        : PSERVER_INFO_100;\n  pWork          : PSERVER_INFO_100;\n  dwEntriesRead  : DWORD;\n  dwTotalEntries : DWORD;\n  i              : integer;\n  dwResult       : NET_API_STATUS;\nbegin\n  Result := TStringList.Create;\n  Result.Clear;\n\n  dwResult := NetServerEnum(nil,100,@pBuffer,MAX_PREFERRED_LENGTH,\n                            @dwEntriesRead,@dwTotalEntries,SV_TYPE_DOMAIN_ENUM,\n                            PWideChar(FDomainName),nil);\n\n  if dwResult = NERR_SUCCESS then begin\n    try\n      pWork := pBuffer;\n      for i := 1 to dwEntriesRead do begin\n        Result.Add(pWork.sv100_name);\n        inc(pWork);\n      end;  //for i\n    finally\n      NetApiBufferFree(pBuffer);\n    end;  //try-finally\n  end  //if no error\n  else begin\n    raise Exception.Create(\'Error while retrieving computer list from domain \' +\n                           FDomainName + #13#10 +\n                           SysErrorMessage(dwResult));\n  end;\nend;\n&lt;snip&gt;\n</code></pre>\n'}, {'answer_id': 8888, 'author': "Shane O'Grady", 'author_id': 1115, 'author_profile': 'https://Stackoverflow.com/users/1115', 'pm_score': 2, 'selected': True, 'text': '<p>You will need to use some LDAP queries</p>\n\n<h2>Here is some code I have used in a previous script (it was taken off the net somewhere, and I\'ve left in the copyright notices)</h2>\n\n<pre><code>\' This VBScript code gets the list of the domains contained in the \n\' forest that the user running the script is logged into\n\n\' ---------------------------------------------------------------\n\' From the book "Active Directory Cookbook" by Robbie Allen\n\' Publisher: O\'Reilly and Associates\n\' ISBN: 0-596-00466-4\n\' Book web site: http://rallenhome.com/books/adcookbook/code.html\n\' ---------------------------------------------------------------\n\nset objRootDSE = GetObject("LDAP://RootDSE")\nstrADsPath =  "&lt;GC://" &amp; objRootDSE.Get("rootDomainNamingContext") &amp; "&gt;;"\nstrFilter  = "(objectcategory=domainDNS);"\nstrAttrs   = "name;"\nstrScope   = "SubTree"\n\nset objConn = CreateObject("ADODB.Connection")\nobjConn.Provider = "ADsDSOObject"\nobjConn.Open "Active Directory Provider"\nset objRS = objConn.Execute(strADsPath &amp; strFilter &amp; strAttrs &amp; strScope)\nobjRS.MoveFirst\nwhile Not objRS.EOF\n    Wscript.Echo objRS.Fields(0).Value\n    objRS.MoveNext\nwend\n</code></pre>\n\n<hr>\n\n<p><a href="http://www.janus-net.de/2005/11/18/getting-list-of-sub-domains-from-active-directory-with-c/" rel="nofollow noreferrer">Also a C# version</a></p>\n'}]
['https://Stackoverflow.com/questions/8880', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/672/']