Results 1 to 6 of 6

Thread: What's My IP address?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    Cheshire, England
    Posts
    116

    What's My IP address?

    Hi, i need to find the ip address of my machine through a Visual Basic app, but i need it to work in winXP/2000

    thx in advance

  2. #2
    jim mcnamara
    Guest

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    Cheshire, England
    Posts
    116
    ok just copied that code into my app, now that dont work sorry, don't know if i've vopied it correctly as their was some moving around to be done, so here, see if theirs any differences, in the code, other wise i'm still open to suggestions:

    Option Explicit

    Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
    Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
    Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
    Private Const ERROR_SUCCESS As Long = 0

    Private Type IP_ADDRESS_STRING
    IpAddr(0 To 15) As Byte
    End Type

    Private Type IP_MASK_STRING
    IpMask(0 To 15) As Byte
    End Type

    Private Type IP_ADDR_STRING
    dwNext As Long
    IpAddress As IP_ADDRESS_STRING
    IpMask As IP_MASK_STRING
    dwContext As Long
    End Type

    Private Type IP_ADAPTER_INFO
    dwNext As Long
    ComboIndex As Long 'reserved
    sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
    sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
    dwAddressLength As Long
    sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
    dwIndex As Long
    uType As Long
    uDhcpEnabled As Long
    CurrentIpAddress As Long
    IpAddressList As IP_ADDR_STRING
    GatewayList As IP_ADDR_STRING
    DhcpServer As IP_ADDR_STRING
    bHaveWins As Long
    PrimaryWinsServer As IP_ADDR_STRING
    SecondaryWinsServer As IP_ADDR_STRING
    LeaseObtained As Long
    LeaseExpires As Long
    End Type

    Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
    (pTcpTable As Any, _
    pdwSize As Long) As Long

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (dst As Any, _
    src As Any, _
    ByVal bcount As Long)
    Private Function LocalIPAddress() As String 'api vars
    Dim cbRequired As Long
    Dim buff() As Byte
    Dim Adapter As IP_ADAPTER_INFO
    Dim AdapterStr As IP_ADDR_STRING
    'working vars Dim ptr1 As Long Dim sIPAddr As String
    Dim found As Boolean

    Call GetAdaptersInfo(ByVal 0&, cbRequired)
    If cbRequired > 0 Then ReDim buff(0 To cbRequired - 1) As Byte
    If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
    'get a pointer to the data stored in buff()
    ptr1 = VarPtr(buff(0))
    Do While (ptr1 <> 0) 'And (found = False)
    'copy the data from the pointer to the
    'first adapter into the IP_ADAPTER_INFO type
    CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
    With Adapter
    'the DHCP IP address is in the
    'IpAddress.IpAddr member
    sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))
    If Len(sIPAddr) > 0 Then
    found = True
    Exit Do
    End If
    ptr1 = .dwNext
    End With 'With Adapter
    'ptr1 is 0 when (no more adapters) Loop 'Do While (ptr1 <> 0)
    End If 'If GetAdaptersInfo End If 'If cbRequired > 0
    'return any string found LocalIPAddress = sIPAddr End Function
    Private Function TrimNull(item As String)

    End Function
    Dim pos As Integer
    'double check that there is a chr$(0) in the string
    pos = InStr(item, Chr$(0))
    If pos Then
    TrimNull = Left$(item, pos - 1)
    Else: TrimNull = item
    End If




    Private Sub Command1_Click()
    Text1.Text = LocalIPAddress()
    End Sub

  4. #4
    jim mcnamara
    Guest
    You need to put the code in a form that has one command button and one textbox. The command button is called Command1, textbox Text1.

    Let me know if you get errors, and what they are, not that it just doesn't work. It does - I copied it and used it a couple of years ago.

  5. #5
    AutoBot
    Guest
    This can also be achieved by adding a winsock control to your form and putting in this code:

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     MsgBox = Winsock1.LocalIP
    4.  
    5. End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    Cheshire, England
    Posts
    116
    aahhh, the winsock solution worked alot easier and basically thx

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