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 "."
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.
AVATOPIA
); and also returns the domain of the logged on user, which can be different from the machinei'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:
Update Two:
Just to be clear what i want is:
Here you go:
#include <Windows.h>
#include <DSRole.h>
#pragma comment(lib, "netapi32.lib")
#include <stdio.h>
int main(int argc, char ** argv)
{
DSROLE_PRIMARY_DOMAIN_INFO_BASIC * info;
DWORD dw;
dw = DsRoleGetPrimaryDomainInformation(NULL,
DsRolePrimaryDomainInfoBasic,
(PBYTE *)&info);
if (dw != ERROR_SUCCESS)
{
wprintf(L"DsRoleGetPrimaryDomainInformation: %u\n", dw);
return dw;
}
if (info->DomainNameDns == NULL)
{
wprintf(L"DomainNameDns is NULL\n");
}
else
{
wprintf(L"DomainNameDns: %s\n", info->DomainNameDns);
}
return 0;
}
Anybody using DsRoleGetPrimaryDomainInformation
in production use should consider calling DsRoleFreeMemory
to free the memory block when the information is no longer needed (as per discussion in the comments).
The function returns three different domain names, e.g.:
stackoverflow.com
stackoverflow.com
STACKOVERFLOW
If the machine is not joined to a domain, then both Forest and dns are blank, with only NetBios name filled with the name of the workgroup, e.g.:
null
null
WORKGROUP
The function also returns a flag indicating if the machine is joined to a domain:
DsRole_RoleMemberWorkstation
: workstation that is a member of a domainDsRole_RoleMemberServer
: server that is a member of a domainDsRole_RolePrimaryDomainController
: primary domain controllerDsRole_RoleBackupDomainController
: backup domain controlleror not:
DsRole_RoleStandaloneWorkstation
: workstation that is not a member of a domainDsRole_RoleStandaloneServer
: server that is not a member of a domainUsing GetComputerNameEx
you can get your computer name and domain name.
Example:
TCHAR local[100];
DWORD hstSize = sizeof(local);
GetComputerNameEx(ComputerNameDnsDomain, local, &hstSize);
Note: ComputerNameDnsDomain
gives domain name and ComputerNameNetBIOS
gives local workgroup(computer) name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With