How can i get the domain name of the machine (if the machine is in fact joined to a domain)?
And, of course, if the machine is not joined to a domain the function should return
null
, or- an empty string, or
- the name of the machine, or
"."
Notes:
the
NetGetJoinInformation
Win32 function returns the legacy NetBIOS name of the domain (e.g.AVATOPIA
), not the name of the domain (e.g.avatopia.local
)the
USERDOMAIN
environment variable returns the domain of the logged on user, which can be different from the machine; and also returns the legacy NetBIOS name of the domain (e.g.AVATOPIA
)the
USERDNSDOMAIN
environment variable returns the domain name of the logged on user, which can be different from the machine
Microsoft has a knowledge base article How to retrieve current user and domain names on Windows NT, Windows 2000, or Windows XP, which relies on getting the user's security token and calling LookupAccountSid.
- the LookupAccountSid Win32 function returns the legacy NetBIOS name of the domain (e.g.
AVATOPIA
); and also returns the domain of the logged on user, which can be different from the machine
Update One
i've also tried using ADs object to bind to the IADs
interface of the domain:
IADs domain;
ADsGetObject("LDAP://rootDES", IDs, out domain);
problem with this approach is that:
- you cannot get the domain name (only the distinguished name)
- it doesn't work if the user does not have permissions to query AD
- it doesn't work if the user is not a valid user in active directory
- it only works for Active Directory domains
Update Two:
Just to be clear what i want is:
- which radio button is selected
- what's in the (selected) box
Bonus Reading
- How to get fully qualified domain name on windows in Delphi (user's domain)
- Machine's domain name in .NET? (from .NET)