Results 1 to 2 of 2

Thread: Finding IP Addresses

  1. #1

    Thread Starter
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    Finding IP Addresses

    What's the best way of getting an IP address from a DNS name?

    I seem to remember a call GetRemoteAddress . . . .
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    VB Code:
    1. ' *** Place the following code inside the form window. ***
    2.  
    3. Private Sub cmdGetIP_Click()
    4.     Dim sockinfo As WSADATA  ' information about Winsock
    5.     Dim hostinfo As HOSTENT  ' information about an Internet host
    6.     Dim pHostinfo As Long    ' pointer to a HOSTENT structure
    7.     Dim pIPAddress As Long   ' pointer to an IP address dword
    8.     Dim ipAddress As Long    ' an IP address, packed into a dword
    9.     Dim pIPString As Long    ' pointer to an IP address formatted as a string
    10.     Dim ipString As String   ' holds a human-readable IP address string
    11.     Dim retval As Long       ' generic return value
    12.    
    13.     ' Open up a Winsock session, using version 2.2.
    14.     retval = WSAStartup(MAKEWORD(2, 2), sockinfo)
    15.     If retval <> 0 Then
    16.         Debug.Print "ERROR: Attempt to open Winsock failed: error"; retval
    17.         Exit Sub
    18.     End If
    19.    
    20.     ' Get information about the domain specified in txtDomain.
    21.     pHostinfo = gethostbyname(txtDomain.Text)
    22.     If pHostinfo = 0 Then
    23.         Debug.Print "Unable to resolve domain name."
    24.     Else
    25.         ' Copy the data into a HOSTENT structure.
    26.         CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo)
    27.         If hostinfo.h_addrtype <> AF_INET Then
    28.             Debug.Print "A non-IP address was returned."
    29.         Else
    30.             ' Copy the pointer to the first (and probably only) IP address in the structure.
    31.             CopyMemory pIPAddress, ByVal hostinfo.h_addr_list, 4
    32.             ' Copy the actual IP address.
    33.             CopyMemory ipAddress, ByVal pIPAddress, 4
    34.             ' Convert the IP address into a human-readable string.
    35.             pIPString = inet_ntoa(ipAddress)
    36.             ' Copy the result into a string variable.
    37.             ipString = Space(lstrlen(pIPString))
    38.             retval = lstrcpy(ipString, pIPString)
    39.             ' Print the result: a human-readable IP address.
    40.             Debug.Print ipString
    41.         End If
    42.     End If
    43.    
    44.     ' Close the Winsock session.
    45.     retval = WSACleanup()
    46. End Sub
    47.  
    48. 'In a module
    49. Public Type WSADATA
    50.     wVersion As Integer
    51.     wHighVersion As Integer
    52.     szDescription As String * 257
    53.     szSystemStatus As String * 129
    54.     iMaxSockets As Long
    55.     iMaxUdpDg As Long
    56.     lpVendorInfo As Long
    57. End Type
    58. Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequested As Integer, lpWSAData _
    59.     As WSADATA) As Long
    60. Public Declare Function WSACleanup Lib "wsock32.dll" () As Long
    61. Public Type HOSTENT
    62.     h_name As Long
    63.     h_aliases As Long
    64.     h_addrtype As Integer
    65.     h_length As Integer
    66.     h_addr_list As Long
    67. End Type
    68. Public Const AF_INET = 2
    69. Public Declare Function gethostbyname Lib "wsock32.dll" (ByVal name As String) As Long
    70. Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal inaddr As Long) As Long
    71. Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
    72.     As Any, ByVal length As Long)
    73. Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
    74. Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _
    75.     lpString2 As Any) As Long
    76.  
    77. Public Function MAKEWORD(ByVal bLow As Byte, ByVal bHigh As Byte) As Integer
    78.     MAKEWORD = Val("&H" & Right("00" & Hex(bHigh), 2) & Right("00" & Hex(bLow), 2))
    79. End Function

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