在 Windows 7 上解析 NetBIOS 名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4750736/
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
Resolve NetBIOS name on Windows 7
提问by Harvey Kwok
According to this link, NetBIOS is no longer supported starting from Windwos Vista. Sure enough, I can no longer see any NetBIOS name from the network properties.
根据此链接,从 Windwos Vista 开始不再支持 NetBIOS。果然,我再也看不到网络属性中的任何 NetBIOS 名称了。
However, when I am writing codes on my Windows 7, I still encounter NetBIOS names in many places. For example
但是,当我在 Windows 7 上编写代码时,我仍然在很多地方遇到 NetBIOS 名称。例如
- I can still use "MYDOMAIN\Harvey" to logon my machine, where I believe MYDOMAIN is a NetBIOS name.
- The environment variables COMPUTERNAME and USERDOMAIN are still NetBIOS names. I am expecting to see a DNS names here
- My SQL Server instance names coming up from my SQL Server Management Studio is still something like MYMACHINE\Instance1.
- 我仍然可以使用“MYDOMAIN\Harvey”来登录我的机器,我相信 MYDOMAIN 是一个 NetBIOS 名称。
- 环境变量 COMPUTERNAME 和 USERDOMAIN 仍然是 NetBIOS 名称。我期待在这里看到 DNS 名称
- 来自 SQL Server Management Studio 的 SQL Server 实例名称仍然类似于 MYMACHINE\Instance1。
I am guessing Microsoft still maintains some pieces of it for backward compatibility. I want to understand how Windows 7 going to resolve the NetBIOS name to an IP address. I found this article explaining how the NetBIOS name resolutionworks but I am afraid this is no longer true in Windows 7. At least there is no WINS server for me.
我猜微软仍然保留其中的一些部分以实现向后兼容性。我想了解 Windows 7 如何将 NetBIOS 名称解析为 IP 地址。我发现这篇文章解释了NetBIOS 名称解析的工作原理,但恐怕这在 Windows 7 中不再适用。至少我没有 WINS 服务器。
My last question is how do I do the NetBIOS name resolution programmatically, preferrably in C#. I am okay to use PInvoke.
我的最后一个问题是如何以编程方式进行 NetBIOS 名称解析,最好是在 C# 中。我可以使用 PInvoke。
UDAPTE
UDAPTE
Tridus was right. I can use System.Net.Dns.GetHostAddresses("hostname") to resolve NetBIOS name. I used reflector to see what's happening under the hood. It is calling gethostbyname() from ws2_32.dll
Tridus 是对的。我可以使用 System.Net.Dns.GetHostAddresses("hostname") 来解析 NetBIOS 名称。我用反射器来看看引擎盖下发生了什么。它从 ws2_32.dll调用gethostbyname()
Here, it explains the gethostbyname() will do NetBIOS name resolution.
在这里,它解释了 gethostbyname() 将进行 NetBIOS 名称解析。
- Check the local host name for a matching name.
- Check the Hosts file for a matching name entry.
- If a DNS server is configured, query it.
- If no match is found, attempt NetBIOS name-resolution.
- 检查本地主机名以获取匹配名称。
- 检查 Hosts 文件以获取匹配的名称条目。
- 如果配置了 DNS 服务器,请查询它。
- 如果未找到匹配项,请尝试 NetBIOS 名称解析。
About the mystery of NetBIOS not supported in this link, I think it just means the API is not supported. People in ServerFaultthink that NetBIOS is still supported in Windows 7.
关于这个链接不支持NetBIOS的奥秘,我认为这只是意味着不支持API。 ServerFault 的人认为 Windows 7 仍然支持 NetBIOS。
采纳答案by Tridus
NetBIOS itself as the old protocol might not be supported, but SMB/CIFS still is and that's why \hostname for filesharing and such still works.
NetBIOS 本身作为旧协议可能不受支持,但 SMB/CIFS 仍然是,这就是为什么 \hostname 用于文件共享等仍然有效。
As for how to resolve a name, I was able to do this:
至于如何解析名称,我可以这样做:
System.Net.Dns.GetHostAddresses("hostname")
System.Net.Dns.GetHostAddresses("主机名")
I'm on a domain so it may be simply appending a DNS suffix and doing a DNS lookup, but it worked for me. Give it a try. :)
我在一个域上,所以它可能只是附加一个 DNS 后缀并进行 DNS 查找,但它对我有用。试一试。:)