3

In my program I am trying to retrieve the domain name of the current machine. i found some code that people wrote in forums but they all seem to work partially.

what i mean is that my domain name is something like 'mydomain.mydomain11.com' and when trying to retrieve the domain name i always get only 'mydomain' instead of 'mydomain.mydomain11'

I also tried usng the 'GetEnvironmentVariable' function which returns the same thing.

If possible, i would like to know how can i retrieve the full path.

my final goal is to query active directory using LDAP protocol, so it will really help to know how can chunk the domain path to something like :

CN=Users,DC=mydomain,DC=mydomain11,DC=com

but getting my full domain name will do for now..

itay312
  • 1,518
  • 4
  • 24
  • 36
  • ok thanks, now i have a string containing the full domain name. is there a way of chunking it the way i used above so i can use it for LDAP qwery? – itay312 Nov 20 '16 at 09:24
  • If you just want to connect to LDAP it's not required to know the domain name or dn, you can just connect to `LDAP://RootDSE`. See https://msdn.microsoft.com/en-us/library/ms677945(v=vs.85).aspx – Remko Nov 20 '16 at 10:06
  • I will want to search for a user/group on this domain later on... – itay312 Nov 20 '16 at 10:11
  • Yes that's what you can use RootDSE for, last link in the RootDSE documentation gives you an example: https://msdn.microsoft.com/en-us/library/ms676736(v=vs.85).aspx – Remko Nov 20 '16 at 10:13
  • Thanks Remko, the example added made it clear to me. – itay312 Nov 20 '16 at 10:37

2 Answers2

4

You can use the GetComputerNameEx function with the ComputerNamePhysicalDnsDomain parameter:

The name of the DNS domain assigned to the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS domain name of the local computer, not the name of the cluster virtual server.`

If your goal is to connect to LDAP to search objects in the directory the usual approach is to connect to RootDSE. Here is an example.

Remko
  • 7,214
  • 2
  • 32
  • 52
  • When the question is listed Delphi Object Pascal, why are all the answers & links reaching out to Microsoft? – Joseph Poirier Sep 24 '19 at 14:40
  • 1
    @JosephPoirier Delphi produces executables that run on the Microsoft Windows operating system (amongst others). Sometimes we need to write code that works directly with the operating system API. – Freddie Bell Sep 25 '19 at 06:25
  • Windows API's are required to read the DNS domain name which the computer is joined to. – Remko Sep 25 '19 at 08:09
  • See this answer for a delphi implementation: https://stackoverflow.com/a/60754717/1855801 – maf-soft Mar 19 '20 at 10:09
0

ref What's the best method for getting the local computer name in Delphi

ref http://www.delphipages.com/forum/showthread.php?t=29823 under LorAn, pronounced Lorne - great framework for fetch

rolled in Delphi variable MAXCHAR for buffer size, should be the largest size that can be returned in shortest timeframe

function getDomain: string;
var
  vlDomainName : array[0..MAXCHAR] of char;
  vlSize : ^DWORD;
begin
  New(vlSize);
  vlSize^ := MAXCHAR;
  ExpandEnvironmentStrings(PChar('%USERDOMAIN%'), vlDomainName, vlSize^);
  Dispose(vlSize);
  Result := vlDomainName;
end;

To get DNSServer.DOMAIN.DOMAIN..

use ADSI library ADSystemInfo GetAnyDCName() function & split on the decimal [.]

Joseph Poirier
  • 386
  • 2
  • 17
  • OP was specifically asking how to get the domain name that the computer is joined to rather than the domain of the logged on user (which can be different in an AD forest or when using trusted domains). Also the `%USERDOMAIN%` variable returns the Netbios domain name (e.g. mydomain rather than mydomain.mydomain11.com). Lastly, why read an environment variable instead of using a proper API that was designed for the purpose? – Remko Sep 25 '19 at 08:11
  • ADSI function for DomainName was returning computername instead of DomainName. Obviously Microsoft created shortcut variables for a reason – Joseph Poirier Sep 25 '19 at 17:32
  • Also, for developing software, I see no reason to run through AD groups that are outside of the user's current login for access. Can you provide a scenario where that would be a requirement? – Joseph Poirier Sep 25 '19 at 17:34
  • ps. Remko. I didn't downvote your answer, I just didn't understand how it was considered complete. It is no more complete than mine is, per the question. – Joseph Poirier Sep 25 '19 at 17:35