Option Explicit
Private Const WSADESCRIPTION_LEN = 256
Private Const WSASYS_STATUS_LEN = 128
Private Type HOSTENT
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(WSADESCRIPTION_LEN) As Byte
szSystemStatus(WSASYS_STATUS_LEN) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Declare Function gethostbyname Lib "wsock32dll" (ByVal szName As String) As Long
Private Declare Function WSAStartup Lib "wsock32dll" (ByVal wVersionRequested As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32dll" () As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function LookupIPAddress(ByVal sHostName As String) As String
Dim wsa As WSADATA
Dim nRet As Long
Dim nTemp As Long
Dim bTemp(0 To 3) As Byte
Dim sOut As String
Dim he As HOSTENT
'Initialize WinSock
WSAStartup &H10, wsa
'Attempt to lookup the host
nRet = gethostbyname(sHostName)
'If it failed, just return nothing
If nRet = 0 Then
sOut = " "
Else
'Take a look at the resulting hostent structure
CopyMemory he, ByVal nRet, Len(he)
'Are there atlest four bytes, then we have
' at least one address
If heh_length >= 4 Then
'Copy the address out,
CopyMemory nTemp, ByVal heh_addr_list, 4
CopyMemory bTemp(0), ByVal nTemp, 4
' and format it
sOut = Format(bTemp(0)) & " " & Format(bTemp(1)) & " " _
& Format(bTemp(2)) & " " & Format(bTemp(3))
Else
sOut = " "
End If
End If
WSACleanup
LookupIPAddress = sOut
End Function
Private Sub Command1_Click()
MsgBox LookupIPAddress("localhost")
End Sub
修改下 API声明 测试代码没问题
获取主机名可以使用下面两种方法:
string machineName = EnvironmentMachineName; (System 命名空间下)
string hostName = DnsGetHostName(); (SystemNet命名空间下)
获取IP地址,(可能需要IPv4 或 IPv6, 有时主机环境会存在多个ip地址)
DnsGetHostAddresses(hostName)可获得IPAddress[] 列表
或者
IPHostEntry hostEntry=DnsGetHostEntry(hostName);
hostEntryAddressList[] 也可以获取IP 地址列表
接下来你可以使用自己的方法筛选获取想要的那个ip地址
比如IPv4 可以使用 hostEntryAddressList[i]AddressFamilyToString() == "InterNetwork" 作为判断条件;
IPv6可以使用hostEntryAddressList[i]AddressFamilyToString() == "InterNetworkV6"
或者使用 hostEntryAddressList[i]IsIPv6LinkLocal 来判断
示例代码网上也能搜的到。
代码仅供参考:
定义两个字符串变量,并且声明获取计算机名称和IP的函数GetLocalHostIPName(CString &sLocalName, CString &sIpAddress)。
CString m_strLocalIP; //本机计算机IPCString m_strLocalName; //本机计算机名
int GetLocalHostIPName(CString &sLocalName, CString &sIpAddress); //获取本机名称IP
定义函数GetHostName(CString &sLocalName, CString &sAddress)。
char name[128];gethostname(name, 128);//获得主机名
当用户使用一个主机名时,系统有以下几个方法查找到其对应的IP地址:
1、在Win10系统中,选择本机连接的无线网络,鼠标右键点击属性,接着在d出的设置里面翻到最下面,就可以看见本机的ip地址了。
2、按住win+r运行,输入cmd,点击确定,在命令符中输入ipconfig敲回车,在最下方就可以看到ip地址。
3、在设置里面打开网络设置,网络属性,可以查询到以太网连接的信息和本地连接的信息状态等
在 NET中获取一台电脑名 IP地址及当前用户名是非常简单 以下是常用的几种方法
在ASP NET中专用属性
获取服务器电脑名 Page Server ManchineName
获取用户信息 Page User
获取客户端电脑名 Page Request UserHostName
获取客户端电脑IP Page Request UserHostAddress
在网络编程中的通用方法
获取当前电脑名 static System Net Dns GetHostName()
根据电脑名取出全部IP地址 static System Net Dns Resolve(电脑名) AddressList
也可根据IP地址取出电脑名 static System Net Dns Resolve(IP地址) HostName
系统环境类的通用属性
当前电脑名 static System Environment MachineName
当前电脑所属网域 static System Environment UserDomainName
lishixinzhi/Article/program/net/201311/11408
以上就是关于vb通过主机名获取ip全部的内容,包括:vb通过主机名获取ip、用C#程序如何获得本地计算机名,本地IP,网络IP、MFC如何获取自己电脑的IP地址和主机名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)