How to get the remote computer name from IP address & How to get the remote system IP-VBForums
Results 1 to 8 of 8

Thread: How to get the remote computer name from IP address & How to get the remote system IP

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    How to get the remote computer name from IP address & How to get the remote system IP

    'How to get the remote computer name from IP address
    'How to get the remote system IP address from computer name
    VB Code:
    1. 'Create a module and paste the following code:
    2.  
    3. Sub Main()
    4.     Dim obj As clsIPRESOLVE    'or your class name that you used
    5.     Set obj = New clsIPRESOLVE    'or your class name that you used
    6.     'To get the remote computer name from IP address
    7.     Debug.Print obj.AddressToName("192.168.0.1")
    8.     'To get the remote system IP address from computer name
    9.     'Debug.Print obj.NameToAddress("WinServer")
    10. End Sub
    11.  
    12. 'To use, create a new class module and paste the following code
    13. 'in. Name it clsIPResolve (or other name you wish):
    14.  
    15. 'Class Module Code starts here:
    16. Private mbInitialized As Boolean
    17. Const WSADescription_Len = 256
    18. Const WSASYS_Status_Len = 128
    19. Const AF_INET = 4&
    20.  
    21. Private Type HOSTENT
    22.     hName As Long
    23.     hAliases As Long
    24.     hAddrType As Integer
    25.     hLength As Integer
    26.     hAddrList As Long
    27. End Type
    28.  
    29. Private Type WSADATA
    30.     wversion As Integer
    31.     wHighVersion As Integer
    32.     szDescription(0 To WSADescription_Len) As Byte
    33.     szSystemStatus(0 To WSASYS_Status_Len) As Byte
    34.     iMaxSockets As Integer
    35.     iMaxUdpDg As Integer
    36.     lpszVendorInfo As Long
    37. End Type
    38.  
    39. Private Declare Function WSAStartup Lib "wsock32" (ByVal VersionReq As Long, WSADataReturn As WSADATA) As Long
    40. Private Declare Function WSACleanup Lib "wsock32" () As Long
    41. Private Declare Function WSAGetLastError Lib "wsock32" () As Long
    42. Private Declare Function gethostbyaddr Lib "wsock32" (addr As Long, addrLen As Long, addrType As Long) As Long
    43. Private Declare Function gethostbyname Lib "wsock32" (ByVal hostname As String) As Long
    44. Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    45.  
    46. Private Sub Class_Initialize()
    47.     Dim wsa As WSADATA
    48.     mbInitialized = (WSAStartup(257, wsa) = 0)
    49. End Sub
    50.  
    51. Private Sub Class_Terminate()
    52.     If mbInitialized Then
    53.         WSACleanup
    54.     End If
    55. End Sub
    56.  
    57. 'checks if string is valid IP address
    58. Private Function CheckIP(IPToCheck As String) As Boolean
    59.     Dim TempValues
    60.     Dim iLoop As Long
    61.     Dim TempByte As Byte
    62.     On Error GoTo CheckIPError
    63.     TempValues = Split(IPToCheck, ".")
    64.     If UBound(TempValues) < 3 Then
    65.         Exit Function
    66.     End If
    67.     For iLoop = LBound(TempValues) To UBound(TempValues)
    68.         TempByte = TempValues(iLoop)
    69.     Next iLoop
    70.     CheckIP = True
    71. CheckIPError:
    72. End Function
    73.  
    74. 'converts IP address from string to sin_addr
    75. Private Function MakeIP(strIP As String) As Long
    76.     Dim vTemp
    77.     Dim lngTemp As Long
    78.     Dim iLoop As Long
    79.     On Error GoTo MakeIPError
    80.     vTemp = Split(strIP, ".")
    81.     For iLoop = 0 To (UBound(vTemp) - 1)
    82.         lngTemp = lngTemp + (vTemp(iLoop) * (256 ^ iLoop))
    83.     Next iLoop
    84.     If vTemp(UBound(vTemp)) < 128 Then
    85.         lngTemp = lngTemp + (vTemp(UBound(vTemp)) * (256 ^ 3))
    86.     Else
    87.         lngTemp = lngTemp + ((vTemp(UBound(vTemp)) - 256) * (256 ^ 3))
    88.     End If
    89.     MakeIP = lngTemp
    90. MakeIPError:
    91. End Function
    92.  
    93. 'resolves IP address to host name
    94. Private Function AddrToName(strAddr As String) As String
    95.     Dim heEntry As HOSTENT
    96.     Dim strHost As String * 255
    97.     Dim strTemp As String
    98.     Dim lngRet As Long
    99.     Dim lngIP As Long
    100.     On Error GoTo AddrToNameError
    101.     If CheckIP(strAddr) Then
    102.         lngIP = MakeIP(strAddr)
    103.         lngRet = gethostbyaddr(lngIP, 4, AF_INET)
    104.         If lngRet = 0 Then
    105.             Exit Function
    106.         End If
    107.         RtlMoveMemory heEntry, lngRet, Len(heEntry)
    108.         RtlMoveMemory ByVal strHost, heEntry.hName, 255
    109.         strTemp = TrimNull(strHost)
    110.         AddrToName = strTemp
    111.     End If
    112. AddrToNameError:
    113. End Function
    114.  
    115. 'resolves host name to IP address
    116. Private Function NameToAddr(ByVal strHost As String)
    117.     Dim ip_list() As Byte
    118.     Dim heEntry As HOSTENT
    119.     Dim strIPAddr As String
    120.     Dim lp_HostEnt As Long
    121.     Dim lp_HostIP As Long
    122.     Dim iLoop As Integer
    123.     On Error GoTo NameToAddrError
    124.     lp_HostEnt = gethostbyname(strHost)
    125.     If lp_HostEnt = 0 Then
    126.         Exit Function
    127.     End If
    128.     RtlMoveMemory heEntry, lp_HostEnt, LenB(heEntry)
    129.     RtlMoveMemory lp_HostIP, heEntry.hAddrList, 4
    130.     ReDim ip_list(1 To heEntry.hLength)
    131.     RtlMoveMemory ip_list(1), lp_HostIP, heEntry.hLength
    132.     For iLoop = 1 To heEntry.hLength
    133.         strIPAddr = strIPAddr & ip_list(iLoop) & "."
    134.     Next
    135.     strIPAddr = Mid(strIPAddr, 1, Len(strIPAddr) - 1)
    136.     NameToAddr = strIPAddr
    137. NameToAddrError:
    138. End Function
    139.  
    140. Public Function AddressToName(strIP As String) As String
    141.     If mbInitialized Then AddressToName = AddrToName(strIP)
    142. End Function
    143.  
    144. Public Function NameToAddress(strName As String) As String
    145.     If mbInitialized Then NameToAddress = NameToAddr(strName)
    146. End Function
    147.  
    148. Private Function TrimNull(sTrim As String) As String
    149.     Dim iFind As Long
    150.     iFind = InStr(1, sTrim, Chr(0))
    151.     If iFind > 0 Then
    152.         TrimNull = Left(sTrim, iFind - 1)
    153.     Else
    154.         TrimNull = sTrim
    155.     End If
    156. End Function
    Please post your feedback if you find any errors on this.

    Thanks

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,746

    Re: How to get the remote computer name from IP address & How to get the remote system IP

    this code is very long can u have short and sweet code

  3. #3
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,746

    Re: How to get the remote computer name from IP address & How to get the remote system IP

    VB Code:
    1. Set obj = New clsIPRESOLVE
    How to use it without sub main..........
    error occuring when using it without sub main...........
    tell me idea that how to use it without sub main


    Thanks

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,590

    Re: How to get the remote computer name from IP address & How to get the remote syste

    you can just put those lines in a sub on a form, e.g.:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim obj As clsIPRESOLVE    'or your class name that you used
    3.     Set obj = New clsIPRESOLVE    'or your class name that you used
    4.     'To get the remote computer name from IP address
    5.     Debug.Print obj.AddressToName("192.168.0.1")
    6.     ' Release the memory
    7.     Set obj = Nothing
    8. End Sub

    This code in post #1 originates from here

  5. #5
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,746

    Re: How to get the remote computer name from IP address & How to get the remote system IP

    thanks bush....

  6. #6
    New Member
    Join Date
    Oct 2007
    Posts
    1

    Re: How to get the remote computer name from IP address & How to get the remote syste

    i use the code of cssriraman but the error is

    Only Comments may appear after End Sub, End Function, or End Property..

  7. #7
    Addicted Member
    Join Date
    Oct 2008
    Posts
    216

    Re: How to get the remote computer name from IP address & How to get the remote syste

    nbtstat -A <IPAddress>
    ping <PCName>

  8. #8
    New Member
    Join Date
    Nov 2009
    Posts
    1

    Re: How to get the remote computer name from IP address & How to get the remote syste

    'Return Hostname from IP
    sName = System.Net.Dns.GetHostEntry("192.168.1.1").HostName

    'Return IP collection from hostname
    aIP = System.Net.Dns.GetHostEntry("ComputerName").AddressList

    for each oIP in aIP
    'step thru the ips checking the class to find the one you were looking for.
    next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.