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

后端 未结 7 691
情话喂你
情话喂你 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:35

    The code in the MSDN sample is a little outdated. This is the function I came up with that works.

    bool ComputerBelongsToDomain()
    {
        bool ret = false;
    
        LSA_OBJECT_ATTRIBUTES objectAttributes;
        LSA_HANDLE policyHandle;
        NTSTATUS status;
        PPOLICY_PRIMARY_DOMAIN_INFO info;
    
        // Object attributes are reserved, so initialize to zeros.
        ZeroMemory(&objectAttributes, sizeof(objectAttributes));
    
        status = LsaOpenPolicy(NULL, &objectAttributes, GENERIC_READ | POLICY_VIEW_LOCAL_INFORMATION, &policyHandle);
        if (!status)
        {
            status = LsaQueryInformationPolicy(policyHandle, PolicyPrimaryDomainInformation, (LPVOID*)&info);
            if (!status)
            {
                if (info->Sid)
                    ret = true;
    
                LsaFreeMemory(info);
            }
    
            LsaClose(policyHandle);
        }
    
        return ret;
    }
    

提交回复
热议问题