Search code examples
delphidelphi-10-seattle

Delphi- How to get my full domain name


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..


Solution

  • 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.