如何以编程方式确定 Windows 计算机是否是域的成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/206172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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.
我需要一种方法来确定运行我的程序的计算机是否已加入任何域。它属于哪个特定域并不重要,重要的是它是否连接到任何东西。我在 vc++ 中针对 Win32 API 进行编码。
采纳答案by Mike Spross
Straight from Microsoft:
直接来自微软:
How To Determine If a Windows NT/Windows 2000 Computer Is a Domain Member
如何确定 Windows NT/Windows 2000 计算机是否是域成员
This approach uses the Windows API. From the article summary:
此方法使用 Windows API。来自文章摘要:
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.
本文介绍如何确定运行 Windows NT 4.0 或 Windows 2000 的计算机是域成员、工作组成员还是使用本地安全机构 API 的独立计算机。
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.
本文还提供了一个小程序的示例代码,该小程序输出运行该程序的计算机是域的一部分、工作组的一部分还是独立计算机。
回答by tzot
I think the NetServerEnumfunction will help you in what you want; I would ask for the primary domain controllers with the SV_TYPE_DOMAIN_CTRL
constant for servertypeparameter. If you don't get any, then you're not in a domain.
我认为NetServerEnum函数会帮助你完成你想要的;我会要求使用servertype参数SV_TYPE_DOMAIN_CTRL
常量的主域控制器。如果你没有得到任何,那么你不在域中。
回答by Ryan Ries
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.
这是基于这样的想法:如果运行此代码的用户当前未登录到域,则 USERDNSDOMAIN 环境变量将为空或不可用。但是,您应该考虑一些注意事项。
Pros:
优点:
- Very easy to implement.
- 99% reliable.
- 非常容易实施。
- 99% 可靠。
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.
- 如果计算机已加入域,则可能会失败或返回错误结果,但执行此代码的用户使用本地帐户登录到该计算机。
- 如果计算机已加入域,但在登录/用户使用缓存凭据登录时与域控制器的网络连接不可用,则可能会失败或返回错误结果。
回答by kgriffs
The code in the MSDN sample is a little outdated. This is the function I came up with that works.
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;
}
回答by kgriffs
You can check the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon for the value of 'CachePrimaryDomain'.
您可以检查注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon 中“CachePrimaryDomain”的值。
回答by sebagomez
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.
编辑:这是一个糟糕的“答案”。我的意思是检查domain\name
计算机名称中的表单。这当然意味着您确实知道域的名称,它并不能解决仅知道计算机是否在任何域中的问题。
回答by sebagomez
Avoid LSA which is a wrong method. You must use DS api (2 lines of code)
避免 LSA,这是一种错误的方法。必须使用DS api(2行代码)