Results 1 to 20 of 20

Thread: Ip address problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Ip address problem

    Ok.. I am like REALLY new to Visual Basics, but I want to create a program that shows your ip in a window, I just dont know how to do it, I have seen a bunch of pages on it, but none help me, please help me on how, and try to explain what each thing does.. thank you in advanced.

  2. #2
    New Member
    Join Date
    May 2005
    Posts
    2

    Re: Ip address problem

    Use CTRL+T and select the "Microsoft Winsock Control". Add the control to a form and a text box. Then use something like this i guess:

    VB Code:
    1. Text1.Text = Winsock1.LocalIP
    Attached Files Attached Files

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Ip address problem

    Here's some code to get your IP address.
    It's straight out of the AllAPI guide.

    Code:
    'This project requires the following components:
    ' - a form (Form1) with a textbox (Text1, Multiline=True)
    '   and a command button (Command1)
    ' - a module (Module1)
    
    'in Form1:
    Private Sub Command1_Click()
        Module1.Start
    End Sub
    
    'In Module1:
    
    '******************************************************************
    'Created By Verburgh Peter.
    ' 07-23-2001
    ' [email protected]
    '-------------------------------------
    'With this small application , you can detect the IP's installed on your computer,
    'including subnet mask , BroadcastAddr..
    '
    'I've wrote this because i've a programm that uses the winsock control, but,
    'if you have multiple ip's  installed on your pc , you could get by using the Listen
    ' method the wrong ip ...
    'Because Winsock.Localip => detects the default ip installed on your PC ,
    ' and in most of the cases it could be the LAN (nic) not the WAN (nic)
    'So then you have to use the Bind function ,to bind to your right ip..
    'but how do you know & find that ip ?
    'you can find it now by this appl.. it check's in the api.. IP Table..
    '******************************************************************
    
    
    Const MAX_IP = 5   'To make a buffer... i dont think you have more than 5 ip on your pc..
    
    Type IPINFO
         dwAddr As Long   ' IP address
        dwIndex As Long '  interface index
        dwMask As Long ' subnet mask
        dwBCastAddr As Long ' broadcast address
        dwReasmSize  As Long ' assembly size
        unused1 As Integer ' not currently used
        unused2 As Integer '; not currently used
    End Type
    
    Type MIB_IPADDRTABLE
        dEntrys As Long   'number of entries in the table
        mIPInfo(MAX_IP) As IPINFO  'array of IP address entries
    End Type
    
    Type IP_Array
        mBuffer As MIB_IPADDRTABLE
        BufferLen As Long
    End Type
    
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
    Sub main()
    Form1.Show
    End Sub
    
    'converts a Long  to a string
    Public Function ConvertAddressToString(longAddr As Long) As String
        Dim myByte(3) As Byte
        Dim Cnt As Long
        CopyMemory myByte(0), longAddr, 4
        For Cnt = 0 To 3
            ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
        Next Cnt
        ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
    End Function
    
    Public Sub Start()
    Dim Ret As Long, Tel As Long
    Dim bBytes() As Byte
    Dim Listing As MIB_IPADDRTABLE
    
    Form1.Text1 = ""
    
    On Error GoTo END1
        GetIpAddrTable ByVal 0&, Ret, True
    
        If Ret <= 0 Then Exit Sub
        ReDim bBytes(0 To Ret - 1) As Byte
        'retrieve the data
        GetIpAddrTable bBytes(0), Ret, False
          
        'Get the first 4 bytes to get the entry's.. ip installed
        CopyMemory Listing.dEntrys, bBytes(0), 4
        'MsgBox "IP's found : " & Listing.dEntrys    => Founded ip installed on your PC..
        Form1.Text1 = Listing.dEntrys & "   IP addresses found on your PC !!" & vbCrLf
        Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf
        For Tel = 0 To Listing.dEntrys - 1
            'Copy whole structure to Listing..
           ' MsgBox bBytes(tel) & "."
            CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
             Form1.Text1 = Form1.Text1 & "IP address                   : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "IP Subnetmask            : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
             Form1.Text1 = Form1.Text1 & "BroadCast IP address  : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf
        Next
    
    'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
    Exit Sub
    END1:
    MsgBox "ERROR"
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: Ip address problem

    Thank you, that last one helped, though I really dont see what each line did, but when I did it, it tells me the ip, but not my internal ip, only external, how would I get the internal also?

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Ip address problem

    Quote Originally Posted by Columbine
    Thank you, that last one helped, though I really dont see what each line did, but when I did it, it tells me the ip, but not my internal ip, only external, how would I get the internal also?
    Whaddya mean by internal/extarnal? As in your router/PC?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: Ip address problem

    Yes, I am on a router, I want it to show both.. ok, what i am wanting to create is a program that shows your system information, ip, HD, ram, so forth.. I know its a hard program for a beginner, but it will teach me alot, and be helpful at the same time, but the ip needs to show BOTH ip's.

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Ip address problem

    Uisng the winsock control as displayed above in post 2 will give you the internal ip address

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: Ip address problem

    I dont know anything about winsock, so thats going to be really hard for me to do I just tried and got nothing.

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Ip address problem

    Its very simple, press control and t and select 'Microsoft Winsock control Version ?' put one on your form then write,

    msgbox winsock1.localip

    I think the origional post (#2) has a program in it download it :-p)

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: Ip address problem

    Its saying

    " Method or Data member not found "

  11. #11
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Talking Re: Ip address problem

    to find your router or public ip you need to use an outside source, I have a page at http://www.planethax.com/ip.shtml.

    then with a form with 2 labels, (wanip & lanip) and to textbox's (test1 & text2)
    then I added this code: (will give you your local ip and your remote ip)

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const ERROR_SUCCESS As Long = 0
    4. Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
    5. Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
    6. Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
    7.  
    8. Private Type IP_ADDRESS_STRING
    9.    IpAddr(0 To 15) As Byte
    10. End Type
    11.  
    12. Private Type IP_MASK_STRING
    13.    IpMask(0 To 15) As Byte
    14. End Type
    15.  
    16. Private Type IP_ADDR_STRING
    17.    dwNext As Long
    18.    IpAddress As IP_ADDRESS_STRING
    19.    IpMask As IP_MASK_STRING
    20.    dwContext As Long
    21. End Type
    22.  
    23. Private Type IP_ADAPTER_INFO
    24.    dwNext As Long
    25.    ComboIndex As Long  'reserved
    26.    sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
    27.    sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
    28.    dwAddressLength As Long
    29.    sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
    30.    dwIndex As Long
    31.    uType As Long
    32.    uDhcpEnabled As Long
    33.    CurrentIpAddress As Long
    34.    IpAddressList As IP_ADDR_STRING
    35.    GatewayList As IP_ADDR_STRING
    36.    DhcpServer As IP_ADDR_STRING
    37.    bHaveWins As Long
    38.    PrimaryWinsServer As IP_ADDR_STRING
    39.    SecondaryWinsServer As IP_ADDR_STRING
    40.    LeaseObtained As Long
    41.    LeaseExpires As Long
    42. End Type
    43.  
    44. Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
    45.   (pTcpTable As Any, _
    46.    pdwSize As Long) As Long
    47.    
    48. Private Declare Sub CopyMemory Lib "kernel32" _
    49.    Alias "RtlMoveMemory" _
    50.   (dst As Any, _
    51.    src As Any, _
    52.    ByVal bcount As Long)
    53.    
    54. Private Declare Function URLDownloadToFile Lib "urlmon" _
    55.    Alias "URLDownloadToFileA" _
    56.   (ByVal pCaller As Long, _
    57.    ByVal szURL As String, _
    58.    ByVal szFileName As String, _
    59.    ByVal dwReserved As Long, _
    60.    ByVal lpfnCB As Long) As Long
    61.    
    62. Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
    63.    Alias "DeleteUrlCacheEntryA" _
    64.   (ByVal lpszUrlName As String) As Long
    65.      
    66. Private Declare Function lstrlenW Lib "kernel32" _
    67.   (ByVal lpString As Long) As Long
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78. Private Sub Form_Load()
    79.  
    80.  
    81.    
    82.    Text1.Text = LocalIPAddress()
    83.    Text2.Text = ""
    84.    Text2.Text = GetPublicIP()
    85. End Sub
    86.  
    87. Private Function DownloadFile(ByVal sURL As String, _
    88.                              ByVal sLocalFile As String) As Boolean
    89.    
    90.    DownloadFile = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = ERROR_SUCCESS
    91.    
    92. End Function
    93.  
    94.  
    95.  
    96. Private Function GetPublicIP()
    97.  
    98.    Dim sSourceUrl As String
    99.    Dim sLocalFile As String
    100.    Dim hfile As Long
    101.    Dim buff As String
    102.    Dim pos1 As Long
    103.    Dim pos2 As Long
    104.    
    105.   'site returning IP address
    106.    sSourceUrl = "http://www.planethax.com/ip.shtml"
    107.    sLocalFile = "c:\ip.txt"
    108.    
    109.   'ensure this file does not exist in the cache
    110.    Call DeleteUrlCacheEntry(sSourceUrl)
    111.  
    112.   'download the public IP file,
    113.   'read into a buffer and delete
    114.    If DownloadFile(sSourceUrl, sLocalFile) Then
    115.    
    116.       hfile = FreeFile
    117.       Open sLocalFile For Input As #hfile
    118.          buff = Input$(LOF(hfile), hfile)
    119.       Close #hfile
    120.  
    121.  
    122.      'look for the IP line
    123.       pos1 = InStr(buff, "var ip =")
    124.    
    125.      'if found,
    126.       If pos1 Then
    127.    
    128.         'get position of first and last single
    129.         'quotes around address (e.g. '11.22.33.44')
    130.          pos1 = InStr(pos1 + 1, buff, "'", vbTextCompare) + 1
    131.          pos2 = InStr(pos1 + 1, buff, "'", vbTextCompare) '- 1
    132.      
    133.         'return the IP address
    134.          GetPublicIP = Mid$(buff, pos1, pos2 - pos1)
    135.  
    136.       Else
    137.      
    138.          GetPublicIP = "(unable to parse IP)"
    139.      
    140.       End If  'pos1
    141.      
    142.       Kill sLocalFile
    143.    
    144.    Else
    145.      
    146.       GetPublicIP = "(unable to access shtml page)"
    147.      
    148.    End If  'DownloadFile
    149.    
    150. End Function
    151.  
    152.  
    153.  
    154.  
    155.  
    156. Private Function LocalIPAddress() As String
    157.    
    158.    Dim cbRequired As Long
    159.    Dim buff() As Byte
    160.    Dim ptr1 As Long
    161.    Dim sIPAddr As String
    162.    Dim Adapter As IP_ADAPTER_INFO
    163.    
    164.    Call GetAdaptersInfo(ByVal 0&, cbRequired)
    165.  
    166.    If cbRequired > 0 Then
    167.    
    168.       ReDim buff(0 To cbRequired - 1) As Byte
    169.      
    170.       If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
    171.      
    172.         'get a pointer to the data stored in buff()
    173.          ptr1 = VarPtr(buff(0))
    174.  
    175.          Do While (ptr1 <> 0)
    176.          
    177.            'copy the data from the pointer to the
    178.            'first adapter into the IP_ADAPTER_INFO type
    179.             CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
    180.          
    181.             With Adapter
    182.                        
    183.               'the DHCP IP address is in the
    184.               'IpAddress.IpAddr member
    185.                  
    186.                sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))
    187.                  
    188.                If Len(sIPAddr) > 0 Then Exit Do
    189.  
    190.                ptr1 = .dwNext
    191.                              
    192.             End With  'With Adapter
    193.            
    194.         'ptr1 is 0 when (no more adapters)
    195.          Loop  'Do While (ptr1 <> 0)
    196.  
    197.       End If  'If GetAdaptersInfo
    198.    End If  'If cbRequired > 0
    199.  
    200.   'return any string found
    201.    LocalIPAddress = sIPAddr
    202.    
    203. End Function
    204.  
    205.  
    206.  
    207.  
    208.  
    209.  Private Function TrimNull(startstr As String) As String
    210.  
    211.    TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
    212.    
    213. End Function

  12. #12
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: Ip address problem

    I can uplode my sample project if you wish

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Ip address problem

    He said he has allready found the public and is looking for internal :-p

    here download the file from here it works fine :-p

    http://www.vbforums.com/showpost.php...34&postcount=2

  14. #14
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: Ip address problem

    ah I see, then yes simple,
    on a form :>projects --> components --> microsoft winsock control

    then a command button and textbox with

    VB Code:
    1. Private Sub Command1_Click()
    2. Text1.Text = Winsock1.LocalIP
    3. End Sub()

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Ip address problem

    Quote Originally Posted by planethax
    VB Code:
    1. End Sub()
    Interesting notation...

    sorry, couldn't resist

  16. #16
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Ip address problem

    you have to close the array of end sub, duh!

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Ip address problem

    Quote Originally Posted by |2eM!x
    you have to close the array of end sub, duh!
    Oh yes, of course

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: Ip address problem

    Ok, I got the internal ip address or the local ip, but if I am on a router, how do I get around that? I am having major problems lol, thanks for all of the help so far though.

  19. #19
    Lively Member CodeBlock's Avatar
    Join Date
    May 2005
    Location
    The_Universe.Milky_Way. Solar_System.Inner_Ring. The_Earth.Asia. India.TN. Chennai. Home_Sweet_Home
    Posts
    85

    Re: Ip address problem

    planethax first wrote:

    ah I see, then yes simple,
    on a form :>projects --> components --> microsoft winsock control

    then a command button and textbox with

    visual basic code:Private Sub Command1_Click()
    Text1.Text = Winsock1.LocalIP
    End Sub()



    Then penagate wrote:
    Quote:
    Originally Posted by planethax

    visual basic code:End Sub()


    Interesting notation...

    sorry, couldn't resist
    And the funniest part is |2eM!x reply, hehe
    Quote Originally Posted by |2eM!x
    you have to close the array of end sub, duh!
    HAHAHAHAHAH

    Quote Originally Posted by penagate
    Oh yes, of course
    Hey, Moderator: why dont u move this post to the Chit Chat's Jokes Section

    Just Playing Around! Anyway, Columbine. First please learn to use Visual Basic and behave like a Programmer and not a Computer User.. plzz .. this is just a request that will help u develop. So, first figure out how to add components Ctrl+T. MORE THAN 3 PPL HAVE EXPLAINED YOU ON HOW TO ADD A CONTROL.

    If you get a "Data Member not found error", then u are not using that control you are supposed to use: "Winsock1" or u have a typo in the Method Name.

    Please keenly look into the Code or just copy paste it to work. And before that read the posts and add the control properly.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     MsgBox WinSock1.LocalIP
    4.  
    5. End Sub ' I Aint becoming a joker again here ... hehe

    Also, please read everypost from top again. You are just replying to the last post and missed many ppl's code.

    Sorry, for any hurts if somehow.

    HTH
    Neo
    Last edited by CodeBlock; May 11th, 2005 at 07:22 AM.

  20. #20
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Ip address problem

    This is an old post but here is the WMI version.. for anyone searching

    Gets the PC's IP Address. (if on router will display internal IP). VB6/Script
    VB Code:
    1. Sub Get_IP()
    2.     Dim objWMIService, IPConfigSet, IPConfig, i
    3.     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    4.     Set IPConfigSet = objWMIService.ExecQuery _
    5.         ("Select IPAddress from Win32_NetworkAdapterConfiguration ")
    6.     For Each IPConfig In IPConfigSet
    7.         If Not IsNull(IPConfig.IPAddress) Then
    8.             For i = LBound(IPConfig.IPAddress) _
    9.                 To UBound(IPConfig.IPAddress)
    10.                     MsgBox IPConfig.IPAddress(i)
    11.             Next
    12.         End If
    13.     Next
    14. End Sub

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