vb6 - 如何根据给定的IP地址获取远程计算机名称

时间:2011-02-28 08:07:56

标签: vb6

如何根据vb6中的给定IP地址获取远程计算机名称?有什么方法可以列出链接到当前计算机的计算机列表吗?

3 个答案:

答案 0 :(得分:3)

如果反向DNS查找执行您想要的操作,这可能会有所帮助。这个例子简化了DNS结果的处理,但是应该让你开始并且可能就足够了:

Option Explicit

Private Const DNS_TYPE_PTR = &HC
Private Const DNS_QUERY_STANDARD = &H0
Private Const DnsFreeRecordListDeep = 1&

Private Enum DNS_STATUS
    ERROR_BAD_IP_FORMAT = -3&
    ERROR_NO_PTR_RETURNED = -2&
    ERROR_NO_RR_RETURNED = -1&
    DNS_STATUS_SUCCESS = 0&
End Enum

Private Type VBDnsRecord
    pNext As Long
    pName As Long
    wType As Integer
    wDataLength As Integer
    Flags As Long
    dwTTL As Long
    dwReserved  As Long
    prt As Long
    others(9) As Long
End Type

Private Declare Function DnsQuery Lib "Dnsapi" Alias "DnsQuery_A" ( _
    ByVal Name As String, _
    ByVal wType As Integer, _
    ByVal Options As Long, _
    ByRef aipServers As Any, _
    ByRef ppQueryResultsSet As Long, _
    ByVal pReserved As Long) As Long

Private Declare Function DnsRecordListFree Lib "Dnsapi" ( _
    ByVal pDnsRecord As Long, _
    ByVal DnsFreeRecordListDeep As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByRef pTo As Any, _
    ByRef uFrom As Any, _
    ByVal lSize As Long)

Private Declare Function StrCopyA Lib "kernel32" Alias "lstrcpyA" ( _
    ByVal retval As String, _
    ByVal PTR As Long) As Long

Private Declare Function StrLenA Lib "kernel32" Alias "lstrlenA" ( _
    ByVal PTR As Long) As Long

Public Function IP2HostName(ByVal IP As String, ByRef HostName As String) As Long
    Dim Octets() As String
    Dim OctX As Long
    Dim NumPart As Long
    Dim BadIP As Boolean
    Dim lngDNSRec As Long
    Dim Record As VBDnsRecord
    Dim Length As Long
    'Returns DNS_STATUS Enum values, otherwise a DNS system error code.

    IP = Trim$(IP)
    If Len(IP) = 0 Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function
    Octets = Split(IP, ".")
    If UBound(Octets) <> 3 Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function
    For OctX = 0 To 3
        If IsNumeric(Octets(OctX)) Then
            NumPart = CInt(Octets(OctX))
            If 0 <= NumPart And NumPart <= 255 Then
                Octets(OctX) = CStr(NumPart)
            Else
                BadIP = True
                Exit For
            End If
        Else
            BadIP = True
            Exit For
        End If
    Next
    If BadIP Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function

    IP = Octets(3) & "." & Octets(2) & "." & Octets(1) & "." & Octets(0) & ".IN-ADDR.ARPA"

    IP2HostName = DnsQuery(IP, DNS_TYPE_PTR, DNS_QUERY_STANDARD, ByVal 0, lngDNSRec, 0)
    If IP2HostName = DNS_STATUS_SUCCESS Then
        If lngDNSRec <> 0 Then
            CopyMemory Record, ByVal lngDNSRec, LenB(Record)

            With Record
                If .wType = DNS_TYPE_PTR Then
                    Length = StrLenA(.prt)
                    HostName = String$(Length, 0)
                    StrCopyA HostName, .prt
                Else
                    IP2HostName = ERROR_NO_PTR_RETURNED
                End If
            End With
            DnsRecordListFree lngDNSRec, DnsFreeRecordListDeep
        Else
            IP2HostName = ERROR_NO_RR_RETURNED
        End If
    'Else
        'Return with DNS error code.
    End If
End Function

但请注意,它不处理NetBIOS名称。

答案 1 :(得分:0)

根据此Microsoft support article,标准GetHostByAddr()函数应该执行此操作。不幸的是,我找不到任何关于如何在VB6中进行GetHostByAddr调用的示例,但也许其他人可以帮助完成该部分。或者,您可以运行nslookup等命令行工具:

bensonk@hunter ~/Desktop/cont $ nslookup 64.34.119.12
Server:     208.67.222.222
Address:    208.67.222.222#53

Non-authoritative answer:
12.119.34.64.in-addr.arpa   name = stackoverflow.com.

这个例子是在linux机器上运行的,但是同样的命令在windows上运行正常。

答案 2 :(得分:0)

最简单的技术

发送电脑名称, 这样做:

创建一个文本框

将其多行属性更改为true。

之后,在该文本框的text属性中,写下:

echo %computername% >> C:\temp.txt

然后,使用Fileinput输入文本文件并输入文本文件C:\temp.txt

如果您想使用此功能通过winsock或LAN发送,请使用winsock1.sendata发送文本框文本

完成