Lynnooi
Lynnooi

Reputation: 199

vb6 - how to get the remote computer name based on the given IP address

how can i get the remote computer name based on a given IP Address in vb6? Is there any way that i can list out a list of computers linked to current computer?

Upvotes: 1

Views: 6418

Answers (3)

shubhamt619
shubhamt619

Reputation: 11

MOST SIMPLE TECHNIQUE EVER

To send computer name , Do this:

Create a textbox,

Change its multiline property to true.

After that, in the text property of that textbox, write this:

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

then, using Fileinput, input the textfile and input the textfile C:\temp.txt.

If you want to use this for sending over winsock or LAN, Send the text box text using winsock1.sendata

Done

Upvotes: 0

Bob77
Bob77

Reputation: 13267

If reverse DNS lookup does what you want this might help. This example simplifies the processing of the DNS results, but should get you started and may be enough:

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

Note however it does not handle NetBIOS names.

Upvotes: 3

Benson
Benson

Reputation: 22837

According to this Microsoft support article, the standard GetHostByAddr() functions should do it. Unfortunately, I can't find any examples of how to do a GetHostByAddr call in VB6, but perhaps someone else can help with that part. Alternately, you could run a commandline tool like 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.

That example was run on a linux machine, but the same command will work fine on windows.

Upvotes: 0

Related Questions