Results 1 to 8 of 8

Thread: How To Get The Remote Computer Name From IP Address

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    1

    How To Get The Remote Computer Name From IP Address

    For all of you out there wondering how to use the gethostbyaddr functionality shown in this vb 6 example: Here is the updated code that works properly in a VB.NET environment. After Several days of searching, reading about API and Data Marshalling, etc, This is the shortest and sweetest working code I have come up with. If you have questions about how this works or why things are the way they are, e-mail me and I will be happy to explain!
    vb.net Code:
    1. Option Explicit On
    2. Imports System.Runtime.InteropServices
    3. Class HNResolver
    4.  
    5.     Public Shared Sub Main()
    6.         Dim resolve As New HNResolver()
    7.         Console.WriteLine(resolve.GetHostNameFromIP("your dotted quad ip address here"))
    8.  
    9.     End Sub
    10.  
    11.     Private Const WSADescription_Len As Long = 256
    12.     Private Const IP_SUCCESS As Integer = 0
    13.     Private Const SOCKET_ERROR As Long = -1
    14.     Private Const AF_INET As Long = 2
    15.     Dim Initialized As Boolean
    16.  
    17.     <StructLayout(LayoutKind.Sequential)> Public Structure WSADATA
    18.         Dim wVersion As Short
    19.         Dim wHighVersion As Short
    20.         <MarshalAs(UnmanagedType.ByValTStr, sizeConst:=WSADescription_Len + 1)> Dim szDescription As String
    21.         <MarshalAs(UnmanagedType.ByValTStr, sizeConst:=WSADescription_Len + 1)> Dim szSystemStatus As String
    22.         Dim iMaxSockets As Integer
    23.         Dim iMaxUdpDg As Integer
    24.         Dim lpVenderInfo As IntPtr
    25.     End Structure
    26.  
    27.     <StructLayout(LayoutKind.Sequential)> Public Class HOSTENT
    28.         Public h_name As IntPtr
    29.         Dim h_aliases As Long
    30.         Dim h_addrtype As Integer
    31.         Dim h_length As Integer
    32.         Dim h_addr_list As Long
    33.     End Class
    34.  
    35.     Private Declare Function WSAStartup Lib "wsock32" _
    36.       (ByVal VersionReq As Int16, _
    37.     ByRef WSADataReturn As WSADATA) As Integer
    38.  
    39.     Private Declare Function WSACleanup Lib "wsock32" () As Long
    40.  
    41.     Private Declare Function inet_addr Lib "wsock32" _
    42.       (ByVal s As String) As Long
    43.  
    44.     Private Declare Function gethostbyaddr Lib "wsock32" _
    45.       (ByRef haddr As Long, _
    46.        ByVal hnlen As Integer, _
    47.        ByVal addrtype As Integer) As IntPtr
    48.  
    49.     Private Declare Function lstrlen Lib "kernel32" _
    50.        Alias "lstrlenA" _
    51.       (ByVal lpString As IntPtr) As Integer
    52.  
    53.     Public Sub New()
    54.         Dim WSAD As New WSADATA()
    55.         If Not WSAStartup(&H201, WSAD) = IP_SUCCESS Then
    56.             Console.WriteLine("Wsa Startup failed")
    57.             Initialized = False
    58.         Else
    59.             Initialized = True
    60.         End If
    61.     End Sub
    62.  
    63.     Private Sub SocketsCleanup()
    64.         If WSACleanup() <> 0 Then
    65.             MsgBox("Windows Sockets error occurred in Cleanup.", vbExclamation)
    66.         End If
    67.     End Sub
    68.  
    69.     Private Function GetHostNameFromIP(ByVal sAddress As String) As String
    70.         Dim host As New HOSTENT
    71.         Dim ptrHost As IntPtr
    72.         Dim hAddress As Long
    73.         Dim nbytes As Integer
    74.  
    75.         If Initialized Then
    76.             hAddress = inet_addr(sAddress)
    77.             If hAddress <> SOCKET_ERROR Then
    78.                 ptrHost = gethostbyaddr(hAddress, 4, AF_INET)
    79.                 System.Runtime.InteropServices.Marshal.PtrToStructure(ptrHost, host)
    80.                 If ptrHost <> IntPtr.Zero Then
    81.                     nbytes = lstrlen(host.h_name)
    82.                     Dim temp(nbytes) As Byte
    83.                     System.Runtime.InteropServices.Marshal.Copy(host.h_name, temp, 0, nbytes - 1)
    84.                     Return System.Text.Encoding.Default.GetString(temp)
    85.                 Else
    86.                     Return "Call to gethostbyaddr failed."
    87.                 End If 'If ptrHosent
    88.                 SocketsCleanup()
    89.             Else
    90.                 Return "String passed is an invalid IP."
    91.             End If 'If hAddress
    92.         Else
    93.             Return "Sockets failed to initialize."
    94.         End If  'If SocketsInitialize
    95.     End Function
    96. End Class
    Last edited by Hack; Oct 21st, 2009 at 05:45 AM. Reason: Added Highlight Tags and split into its own thread from the VB6 CodeBank thread

  2. #2
    New Member
    Join Date
    Oct 2009
    Location
    India
    Posts
    3

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

    Hi Sevenyb420,
    I am sivakumar a new user, looked at your code, which matched my requirements. it worked for most of the ip addresses to resolve or get the computer name. But very few of the systems in my network i am getting the argument null exception:

    "Value cannot be null. Parameter name: ptr"

    this exception occured at line :

    System.Runtime.InteropServices.Marshal.PtrToStructure(ptrHost, host)
    in GetHostNameFromIP method.

    Can you please let me know the solution. If you need any more details please feel free to ask.

    Thanks in advance
    Siva kumar V

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How To Get The Remote Computer Name From IP Address

    From the MSDN documentation on the gethostbyaddr function:

    Note The capability to perform reverse lookups using the gethostbyaddr function is convenient, but such lookups are considered inherently unreliable, and should be used only as a hint.
    So I would say you should probably look for another way to do this reverse lookup you need to do. One way would be to just use Process.Start to launch the NSLOOKUP program that is included in Windows and parse the output. Another way would be to build a DNS query and send it to your DNS server via UDP but that would be quite a lot of work.

    In response to your actual problem though, the MSDN documentation also says this:
    If no error occurs, gethostbyaddr returns a pointer to the hostent structure. Otherwise, it returns a null pointer, and a specific error code can be retrieved by calling WSAGetLastError.
    MSDN article in question: http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    New Member
    Join Date
    Oct 2009
    Location
    India
    Posts
    3

    Re: How To Get The Remote Computer Name From IP Address

    Hi chris128,
    Thanks for your reply. I am working on that. Before that i found one Weird thing from Sevenyb420 code, when ever i execute the code ( GetHostNameFromIP method) i am getting the system name with out last letter. for example system name is "sivakumar.xx.com", but code returning the system name as "sivakumar.xx.co" . Do you have any clue why it is showing like that?

    Thanks
    Siva kumar V

  5. #5
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: How To Get The Remote Computer Name From IP Address

    There is another way of doing this!

    I recently developed an online API (if you can call it that) that displays the IP. I use it myself, so it will always be there.

    Try going to http://flamefusion.net/API/InternetProtocolAddress.aspx to see what I mean.

    The page only contains your IP, and nothing else. Just download that file with a System.Net.HttpWebRequest or a System.Net.WebClient depending on what you prefer, and you've got the IP!

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How To Get The Remote Computer Name From IP Address

    That is an external IP, I dont think that is what Siva is after
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: How To Get The Remote Computer Name From IP Address

    Quote Originally Posted by chris128 View Post
    That is an external IP, I dont think that is what Siva is after
    Oh! My bad. I got confused by the word "Remote" in the title of the topic.

  8. #8
    New Member
    Join Date
    Oct 2009
    Location
    India
    Posts
    3

    Re: How To Get The Remote Computer Name From IP Address

    Hi Chris 128,
    Thanks for immediate reply. I got the solution for this problem.
    In the line number 83 following code has been written:
    System.Runtime.InteropServices.Marshal.Copy(host.h_name, temp, 0, nbytes - 1)

    i donot know why they have written nbytes-1, after removing "-1" I got the system name comng properly.
    Any how thanks alot for your helping hand.

    Regards
    Siva Kumar V
    Last edited by siva_vandanapu; Oct 22nd, 2009 at 07:54 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width