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

后端 未结 7 684
情话喂你
情话喂你 2020-12-31 09:43

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 connecte

7条回答
  •  醉话见心
    2020-12-31 10:26

    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.

    Pros:

    • Very easy to implement.
    • 99% reliable.

    Cons:

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

提交回复
热议问题