Results 1 to 2 of 2

Thread: How to findIP address assigned by my ISP?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Planet Earth
    Posts
    2

    Post How to findIP address assigned by my ISP?

    I am using a single winsock control for my application which has to run on a maching which has multiple network cards. The same machine is connected to internet and gets assigned IP address from the ISP each time it connects. The internet connection is a DSL connection through USB port.

    The application needs to monitor the IP address given to the machine by ISP. How to single out this address??
    Should I need to assign winsock to every network adapter??
    Avoid Life...It kills you in the end!!

  2. #2
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    This is from API-Guide 3.7:

    VB Code:
    1. 'This project requires the following components:
    2. ' - a form (Form1) with a textbox (Text1, Multiline=True)
    3. '   and a command button (Command1)
    4. ' - a module (Module1)
    5.  
    6. 'in Form1:
    7. Private Sub Command1_Click()
    8.     Module1.Start
    9. End Sub
    10.  
    11. 'In Module1:
    12.  
    13. '******************************************************************
    14. 'Created By Verburgh Peter.
    15. ' 07-23-2001
    16. ' [email][email protected][/email]
    17. '-------------------------------------
    18. 'With this small application , you can detect the IP's installed on your computer,
    19. 'including subnet mask , BroadcastAddr..
    20. '
    21. 'I've wrote this because i've a programm that uses the winsock control, but,
    22. 'if you have multiple ip's  installed on your pc , you could get by using the Listen
    23. ' method the wrong ip ...
    24. 'Because Winsock.Localip => detects the default ip installed on your PC ,
    25. ' and in most of the cases it could be the LAN (nic) not the WAN (nic)
    26. 'So then you have to use the Bind function ,to bind to your right ip..
    27. 'but how do you know & find that ip ?
    28. 'you can find it now by this appl.. it check's in the api.. IP Table..
    29. '******************************************************************
    30.  
    31.  
    32. Const MAX_IP = 5   'To make a buffer... i dont think you have more than 5 ip on your pc..
    33.  
    34. Type IPINFO
    35.      dwAddr As Long   ' IP address
    36.     dwIndex As Long '  interface index
    37.     dwMask As Long ' subnet mask
    38.     dwBCastAddr As Long ' broadcast address
    39.     dwReasmSize  As Long ' assembly size
    40.     unused1 As Integer ' not currently used
    41.     unused2 As Integer '; not currently used
    42. End Type
    43.  
    44. Type MIB_IPADDRTABLE
    45.     dEntrys As Long   'number of entries in the table
    46.     mIPInfo(MAX_IP) As IPINFO  'array of IP address entries
    47. End Type
    48.  
    49. Type IP_Array
    50.     mBuffer As MIB_IPADDRTABLE
    51.     BufferLen As Long
    52. End Type
    53.  
    54. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    55. Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
    56. Sub main()
    57. Form1.Show
    58. End Sub
    59.  
    60. 'converts a Long  to a string
    61. Public Function ConvertAddressToString(longAddr As Long) As String
    62.     Dim myByte(3) As Byte
    63.     Dim Cnt As Long
    64.     CopyMemory myByte(0), longAddr, 4
    65.     For Cnt = 0 To 3
    66.         ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
    67.     Next Cnt
    68.     ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
    69. End Function
    70.  
    71. Public Sub Start()
    72. Dim Ret As Long, Tel As Long
    73. Dim bBytes() As Byte
    74. Dim Listing As MIB_IPADDRTABLE
    75.  
    76. Form1.Text1 = ""
    77.  
    78. On Error GoTo END1
    79.     GetIpAddrTable ByVal 0&, Ret, True
    80.  
    81.     If Ret <= 0 Then Exit Sub
    82.     ReDim bBytes(0 To Ret - 1) As Byte
    83.     'retrieve the data
    84.     GetIpAddrTable bBytes(0), Ret, False
    85.      
    86.     'Get the first 4 bytes to get the entry's.. ip installed
    87.     CopyMemory Listing.dEntrys, bBytes(0), 4
    88.     'MsgBox "IP's found : " & Listing.dEntrys    => Founded ip installed on your PC..
    89.     Form1.Text1 = Listing.dEntrys & "   IP addresses found on your PC !!" & vbCrLf
    90.     Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf
    91.     For Tel = 0 To Listing.dEntrys - 1
    92.         'Copy whole structure to Listing..
    93.        ' MsgBox bBytes(tel) & "."
    94.         CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
    95.          Form1.Text1 = Form1.Text1 & "IP address                   : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
    96.          Form1.Text1 = Form1.Text1 & "IP Subnetmask            : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
    97.          Form1.Text1 = Form1.Text1 & "BroadCast IP address  : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
    98.          Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf
    99.     Next
    100.  
    101. 'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
    102. Exit Sub
    103. END1:
    104. MsgBox "ERROR"
    105. End Sub


    -Lou

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