Results 1 to 11 of 11

Thread: Current IP Address

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Current IP Address

    How do I get the current IP address I am on using VB code?

    Thanks
    David Wilhelm

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Easier way?

    Is there an easier way to do this?

    All I want is the default IP address. I don't need the subnet mask or the default gateway.

    Thanks
    David Wilhelm

  4. #4
    Member
    Join Date
    Nov 2002
    Location
    Michigan
    Posts
    54

    Get IP address

    If you are looking at only getting the IP address then add the winsock control and then the local ip is

    VB Code:
    1. Private Sub Form_Load()
    2.      strLocalIP = Winsock1.LocalIP
    3.      Msgbox "Your Local IP is " & strLocalIP,vbInformation,"Local IP"
    4. End Sub

  5. #5
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    Re: Easier way?

    Originally posted by indydavid32
    Is there an easier way to do this?

    All I want is the default IP address. I don't need the subnet mask or the default gateway.

    Thanks
    Go to ...


    http://www.mvps.org/vbnet/index.html...lipaddress.htm

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Current active IP address

    My parameters have suddenly changed.

    Now I need to bring back the current active IP address.

    Here at work the IP address on laptops will be in one range. At home the ip address will be differant.

    Is there a way to find the current active IP address?

    Thanks for the help.
    David Wilhelm

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Based on kleinma's post, here you are....

    To make it easier, you would only need this on the Form's code:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim Address() As String, SubNet() As String, BroadCast() As String    
    3.     GetIPs Address(), SubNet(), BroadCast()
    4.    
    5.      MsgBox "DefaultIP: " & Address(0)    
    6. End Sub
    and the module I've provided.
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Get IP address

    Originally posted by rdrouill
    If you are looking at only getting the IP address then add the winsock control and then the local ip is

    VB Code:
    1. Private Sub Form_Load()
    2.      strLocalIP = Winsock1.LocalIP
    3.      Msgbox "Your Local IP is " & strLocalIP,vbInformation,"Local IP"
    4. End Sub
    That would mean that your app needs of the Winsock to be installed on the user's machine. I don't think this is a brightest idea.... unless you're already using this control on your project.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    or you could just shell out to a command prompt saving yourself some coding ...
    VB Code:
    1. Private Sub Form_Load()
    2.     MsgBox GetLocalIPAddress
    3. End Sub
    4.  
    5. Private Function GetLocalIPAddress() As String
    6.     Shell "Command.com /c IPConfig > c:\Output.txt"
    7.  
    8.     Open "c:\Output.txt" For Input As #1
    9.         GetLocalIPAddress = Input(LOF(1), 1)
    10.     Close #1
    11.    
    12.     GetLocalIPAddress = CStr(Mid(GetLocalIPAddress, _
    13.     InStr(GetLocalIPAddress, ". . : ") + 6, Len(GetLocalIPAddress)))
    14.     GetLocalIPAddress = CStr(Trim(CStr(Left(GetLocalIPAddress, _
    15.     InStr(GetLocalIPAddress, Chr(13)) - 1))))
    16. End Function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Re: Get IP address

    Originally posted by Mc Brain
    That would mean that your app needs of the Winsock to be installed on the user's machine. I don't think this is a brightest idea.... unless you're already using this control on your project.
    yup.. plus i think the winsock control uses the function i posted anyway.. it is just a wrapper around the winsock API functions..

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Talking Thanks

    Thanks for all the help everyone!
    David Wilhelm

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