How do you programmatically determine whether a Windows computer is a member of a domain?

By : kgriffs

I need a way to determine whether the computer running my program is joined to any domain. It doesn't matter what specific domain it is part of, just whether it is connected to anything. I'm coding in vc++ against the Win32 API.

By : kgriffs


Here is a dead simple approach I don't see mentioned.

TCHAR UserDnsDomain[128] = { 0 }; 
DWORD Result = 0;

Result = GetEnvironmentVariable("USERDNSDOMAIN", UserDnsDomain, sizeof(UserDnsDomain));

if (Result == 0 || Result >= sizeof(UserDnsDomain) || GetLastError() == ERROR_ENVVAR_NOT_FOUND)
    return(FALSE); // Not logged in to a domain

This is predicated on the idea that if the user who is running this code is not currently logged in to a domain, then the USERDNSDOMAIN environment variable will be empty or unavailable. But there are some caveats you should think about.


  • Very easy to implement.
  • 99% reliable.


  • May fail or return false results if the computer is domain joined, but the user executing this code is logged on to that computer with a local account.
  • May fail or return false results if the computer is domain joined, but network connectivity to a domain controller was unavailable at the time of logon/user logged on with cached credentials.
By : Ryan Ries

what about from the name of the computer?

edit: this was a crapy 'answer' from way back. What I meant was cheching for the form domain\name in the computer name. That of course implies that you do know the name of the domain, it does not solves the issue of just knowing if the computer is in any domain.

By : sebagomez

This video can help you solving your question :)
By: admin