如何以编程方式确定 Windows 计算机是否是域的成员?

发布于 2024-07-06 23:23:15 字数 97 浏览 5 评论 0原文

我需要一种方法来确定运行我的程序的计算机是否已加入任何域。 它属于哪个特定域并不重要,重要的是它是否连接到任何东西。 我正在针对 Win32 API 使用 vc++ 进行编码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浮世清欢 2024-07-13 23:23:15

直接来自 Microsoft:

如何确定 Windows NT/Windows 2000 计算机是否是域成员

此方法使用 Windows API。 从文章摘要来看:

本文介绍了如何
确定计算机是否是
运行 Windows NT 4.0 或 Windows 2000
是域的成员,是成员
一个工作组,或者是一个独立的
使用本地安全的计算机
权威API。

本文还提供了一个小程序的示例代码,该程序输出运行该程序的计算机是域的一部分、工作组的一部分还是独立计算机。

Straight from Microsoft:

How To Determine If a Windows NT/Windows 2000 Computer Is a Domain Member

This approach uses the Windows API. From the article summary:

This article describes how to
determine if a computer that is
running Windows NT 4.0 or Windows 2000
is a member of a domain, is a member
of a workgroup, or is a stand-alone
computer using the Local Security
Authority APIs.

The article also provides sample code for a small program that outputs whether the computer the program is running on is part of a domain, part of a workgroup, or a standalone computer.

葬心 2024-07-13 23:23:15

我认为 NetServerEnum 函数会对您有所帮助你想要什么; 我会要求主域控制器的 servertype 参数带有 SV_TYPE_DOMAIN_CTRL 常量。 如果您没有收到任何信息,则说明您不在域中。

I think the NetServerEnum function will help you in what you want; I would ask for the primary domain controllers with the SV_TYPE_DOMAIN_CTRL constant for servertype parameter. If you don't get any, then you're not in a domain.

Oo萌小芽oO 2024-07-13 23:23:15

这是一个我没有看到提到的非常简单的方法。

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
}

这是基于这样的想法:如果运行此代码的用户当前未登录到域,则 USERDNSDOMAIN 环境变量将为空或不可用。 但您应该考虑一些注意事项。

优点:

  • 非常容易实施。
  • 99% 可靠。

缺点:

  • 如果计算机已加入域,但执行此代码的用户使用本地帐户登录到该计算机,则可能会失败或返回错误结果。
  • 如果计算机已加入域,但在登录/用户使用缓存凭据登录时与域控制器的网络连接不可用,则可能会失败或返回错误结果。

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.
长亭外,古道边 2024-07-13 23:23:15

MSDN 示例中的代码有点过时。 这是我想出的有效函数。

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;
}

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;
}
芯好空 2024-07-13 23:23:15

您可以检查注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon 中“CachePrimaryDomain”的值。

You can check the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon for the value of 'CachePrimaryDomain'.

苍风燃霜 2024-07-13 23:23:15

避免LSA,这是一种错误的方法。
必须使用DS api(2行代码)

Avoid LSA which is a wrong method.
You must use DS api (2 lines of code)

子栖 2024-07-13 23:23:15

从计算机的名称来看呢?

编辑:这是一个很糟糕的“答案”。 我的意思是检查计算机名称中的domain\name 形式。 这当然意味着您确实知道域的名称,它并不能解决仅知道计算机是否位于任何域中的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文