Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

Get IP from Host Name.

Total Hit ( 3527)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article



This example will show you how to get IP address from host name and also How to retrive all associated IPs to your local machine.

Click here to copy the following block
'Copy Following code in form

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 Const AF_INET = 2
Private Declare Function gethostbyname _
    Lib "WSOCK32.DLL" (ByVal name As String) As Long
Private Declare Function gethostname _
    Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
Private Declare Function inet_ntoa _
    Lib "WSOCK32.DLL" (ByVal inaddr As Long) As Long
Private Declare Sub CopyMemory _
    Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any _
    , Source As Any, ByVal length As Long)
Private Declare Function lstrlen _
    Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function lstrcpy _
    Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _
    lpString2 As Any) As Long

Private Sub Form_Load()
  MsgBox GetIpFromName("")
  MsgBox GetAllLocalIP
End Sub

Function GetIpFromName(hostname As String) As String
  Dim hostinfo As HOSTENT                ' information about an Internet host
  Dim pHostinfo As Long                 ' pointer to a HOSTENT structure
  Dim pIPAddress As Long                 ' pointer to an IP address dword
  Dim ipAddress As Long                 ' an IP address, packed into a dword
  Dim pIPString As Long                 ' pointer to an IP address formatted as a string
  Dim ipString As String                 ' holds a human-readable IP address string
  Dim retval As Long                   ' generic return value

  'HostName = InputBox("Enter hostname to resolve", "[Hostname to IP demo]", "")

  ' Get information about the domain specified in txtDomain.
  pHostinfo = gethostbyname(hostname)
  If pHostinfo = 0 Then
    'MsgBox "Unable to resolve domain name."
    'Copy the data into a HOSTENT structure.
    CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo)
    If hostinfo.h_addrtype <> AF_INET Then
      'MsgBox "A non-IP address was returned."
      Debug.Print "A non-IP address was returned."
      ' Copy the pointer to the first (and probably only) IP address in the structure.
      CopyMemory pIPAddress, ByVal hostinfo.h_addr_list, 4

      ' Copy the actual IP address.
      CopyMemory ipAddress, ByVal pIPAddress, 4
      ' Convert the IP address into a human-readable string.
      pIPString = inet_ntoa(ipAddress)
      ' Copy the result into a string variable.
      ipString = Space(lstrlen(pIPString))
      retval = lstrcpy(ipString, pIPString)
      ' Print the result: a human-readable IP address.
      'MsgBox HostName & " >> " & ipString
      GetIpFromName = ipString
    End If
  End If

End Function

Private Sub GetAllLocalIP()
  Dim hostname As String * 256
  Dim hostent_addr As Long
  Dim host As HOSTENT
  Dim hostip_addr As Long
  Dim temp_ip_address() As Byte
  Dim i As Integer
  Dim ip_address As String

  ' local machine name
  If gethostname(hostname, 256) = -1 Then
    MsgBox "Windows Sockets error "          '& Str(WSAGetLastError())
    Exit Sub
    hostname = Trim$(hostname)
  End If

  hostent_addr = gethostbyname(hostname)

  If hostent_addr = 0 Then
    MsgBox "Winsock.dll is not responding."
    Exit Sub
  End If

  CopyMemory host, ByVal hostent_addr, LenB(host)
  CopyMemory hostip_addr, ByVal host.h_addr_list, 4

  MsgBox hostname

  'get all of the IP address if machine is multi-homed
    ReDim temp_ip_address(1 To host.h_length)
    CopyMemory temp_ip_address(1), ByVal hostip_addr, host.h_length

    For i = 1 To host.h_length
      ip_address = ip_address & temp_ip_address(i) & "."
    ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)

    MsgBox ip_address

    ip_address = ""
    host.h_addr_list = host.h_addr_list + LenB(host.h_addr_list)
    CopyMemory hostip_addr, ByVal host.h_addr_list, 4
  Loop While (hostip_addr <> 0)

End Sub

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.