Results 1 to 6 of 6

Thread: Find Local IP Address

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    7
    I'm trying to make an internet chat program, but I'm having trouble getting my IP address. I know you can use the winsock control's localIP property to return it, but whenever I use it, it returns the IP address of my network card (192.168.0.1), which does me know good. I need it to tell me the IP of my PPP adapter (internet connection). Does anyone know how to get that or how to switch the IP that winsock looks to?

  2. #2
    Member FirePoweR's Avatar
    Join Date
    Apr 1999
    Posts
    32
    Try this:
    Connect to something on the internet (www.vb-world.net port 80) and then check the local ip. The local ip should change.

    Code:
    Winsock1.Close
    Winsock1.Connect "www.vb-world.net", 80
    While Winsock1.State <> sckConnected
        DoEvents
    Wend
    NewIpAddr = Winsock1.LocalIP
    Winsock1.Close

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137

    Any way to do without the winsock control?

    Does any one have any ideas on how to do this without using the winsock control?


    Ie using the registry or an API Call?

    regards


    Matt Brown
    Senior Developer
    Nova Solutions
    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  4. #4
    Junior Member
    Join Date
    Jan 2000
    Posts
    21

    Talking the most worthless ways to....

    You can use ipconfig!
    ipconfig /batch test.txt

    Then you will have the ip in test.txt!

    I have actually used this!!!

  5. #5
    Lively Member
    Join Date
    Apr 2000
    Posts
    110

    Smile Sure can through API

    You can get the local IP through code. I posted it a few days ago as a little network/internet class module. Many other people have also posted the code before. In future, you might find it useful to search the forums before posting for help... Anyway, here is the code i posted:

    Create a new Class file and name it "Net". Put the following code into it.

    Code:
    'Net.cls
    Option Explicit
    
    Private Const INTERNET_CONNECTION_LAN As Long = &H2
    Private Const INTERNET_CONNECTION_MODEM As Long = &H1
    
    Private Const NCBASTAT = &H33
    Private Const NCBNAMSZ = 16
    Private Const HEAP_ZERO_MEMORY = &H8
    Private Const HEAP_GENERATE_EXCEPTIONS = &H4
    Private Const NCBRESET = &H32
    
    Private Const MAX_WSADescription = 256
    Private Const MAX_WSASYSStatus = 128
    Private Const ERROR_SUCCESS As Long = 0
    Private Const WS_VERSION_REQD As Long = &H101
    Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ & H100  And &HFF&
    Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
    Private Const MIN_SOCKETS_REQD As Long = 1
    Private Const SOCKET_ERROR As Long = -1
    
    Private Type NCB
      ncb_command As Byte
      ncb_retcode As Byte
      ncb_lsn As Byte
      ncb_num As Byte
      ncb_buffer As Long
      ncb_length As Integer
      ncb_callname As String * NCBNAMSZ
      ncb_name As String * NCBNAMSZ
      ncb_rto As Byte
      ncb_sto As Byte
      ncb_post As Long
      ncb_lana_num As Byte
      ncb_cmd_cplt As Byte
      ncb_reserve(9) As Byte ' Reserved, must be 0
      ncb_event As Long
    End Type
    
    Private Type ADAPTER_STATUS
      adapter_address(5) As Byte
      rev_major As Byte
      reserved0 As Byte
      adapter_type As Byte
      rev_minor As Byte
      duration As Integer
      frmr_recv As Integer
      frmr_xmit As Integer
      iframe_recv_err As Integer
      xmit_aborts As Integer
      xmit_success As Long
      recv_success As Long
      iframe_xmit_err As Integer
      recv_buff_unavail As Integer
      t1_timeouts As Integer
      ti_timeouts As Integer
      Reserved1 As Long
      free_ncbs As Integer
      max_cfg_ncbs As Integer
      max_ncbs As Integer
      xmit_buf_unavail As Integer
      max_dgram_size As Integer
      pending_sess As Integer
      max_cfg_sess As Integer
      max_sess As Integer
      max_sess_pkt_size As Integer
      name_count As Integer
    End Type
    
    Private Type NAME_BUFFER
      name As String * NCBNAMSZ
      name_num As Integer
      name_flags As Integer
    End Type
    
    Private Type ASTAT
      adapt As ADAPTER_STATUS
      NameBuff(30) As NAME_BUFFER
    End Type
    
    Private Type HostEnt
       hName      As Long
       hAliases   As Long
       hAddrType  As Integer
       hLen       As Integer
       hAddrList  As Long
    End Type
    
    Private Type WSADATA
       wVersion      As Integer
       wHighVersion  As Integer
       szDescription(0 To MAX_WSADescription)   As Byte
       szSystemStatus(0 To MAX_WSASYSStatus)    As Byte
       wMaxSockets   As Integer
       wMaxUDPDG     As Integer
       dwVendorInfo  As Long
    End Type
    
    
    Private Declare Function InternetGetConnectedState _
    Lib "wininet.dll" (ByRef lpSFlags As Long, _
    ByVal dwReserved As Long) As Long
    
    Private Declare Function Netbios Lib "netapi32.dll" _
    (pncb As NCB) As Byte
    
    Private Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" (hpvDest As Any, _
    ByVal hpvSource As Long, ByVal cbCopy As Long)
    
    Private Declare Function GetProcessHeap Lib "kernel32" _
    () As Long
    
    Private Declare Function HeapAlloc Lib "kernel32" _
    (ByVal hHeap As Long, ByVal dwFlags As Long, _
    ByVal dwBytes As Long) As Long
    
    Private Declare Function HeapFree Lib "kernel32" _
    (ByVal hHeap As Long, ByVal dwFlags As Long, _
    lpMem As Any) As Long
    
    Private Declare Function WSAGetLastError Lib "wsock32.dll" _
    () As Long
    
    Private Declare Function WSAStartup Lib "wsock32.dll" _
       (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
       
    Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
    
    Private Declare Function gethostname Lib "wsock32.dll" _
       (ByVal szHost As String, ByVal dwHostLen As Long) As Long
       
    Private Declare Function gethostbyname Lib "wsock32.dll" _
       (ByVal szHost As String) As Long
    
    '~Function for retrieving the MAC or ethernet address
    ' from the networked machine. On error, the returned string
    ' will equal 8 zeros.
    Public Function GetEthernet() As String
    
        Dim udtNCB       As NCB
        Dim bytResponse  As Byte
        Dim udtASTAT     As ASTAT
        Dim udtTempASTAT As ASTAT
        Dim lngASTAT     As Long
        Dim strOut       As String
        Dim x            As Integer
    
            udtNCB.ncb_command = NCBRESET
            bytResponse = Netbios(udtNCB)
            udtNCB.ncb_command = NCBASTAT
            udtNCB.ncb_lana_num = 0
            udtNCB.ncb_callname = "* "
            udtNCB.ncb_length = Len(udtASTAT)
            lngASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, udtNCB.ncb_length)
            strOut = ""
      
        If lngASTAT Then
      
            udtNCB.ncb_buffer = lngASTAT
            bytResponse = Netbios(udtNCB)
            
            CopyMemory udtASTAT, udtNCB.ncb_buffer, Len(udtASTAT)
         
            With udtASTAT.adapt
                
                For x = 0 To 5
                    
                    strOut = strOut & Right$("00" & Hex$(.adapter_address(x)), 2)
                
                Next x
        
            End With
        
            HeapFree GetProcessHeap(), 0, lngASTAT
            
        End If
      
      GetEthernet = strOut
      
    End Function
    
    Public Function GetIPAddress() As String
    
       Dim sHostName    As String * 256
       Dim lpHost    As Long
       Dim Host      As HostEnt
       Dim dwIPAddr  As Long
       Dim tmpIPAddr() As Byte
       Dim i         As Integer
       Dim sIPAddr  As String
       
        If Not SocketsInitialize() Then
            GetIPAddress = ""
            Exit Function
        End If
       
        If gethostname(sHostName, 256) = SOCKET_ERROR Then
            GetIPAddress = ""
            'MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
            '      " has occurred. Unable to successfully get Host Name."
            SocketsCleanup
            Exit Function
        End If
       
            sHostName = Trim$(sHostName)
            lpHost = gethostbyname(sHostName)
        
        If lpHost = 0 Then
            GetIPAddress = ""
            'MsgBox "Windows Sockets are not responding. " & _
            '    "Unable to successfully get Host Name."
            SocketsCleanup
            Exit Function
        End If
       
       CopyMemory Host, lpHost, Len(Host)
       CopyMemory dwIPAddr, Host.hAddrList, 4
       
       ReDim tmpIPAddr(1 To Host.hLen)
       
       CopyMemory tmpIPAddr(1), dwIPAddr, Host.hLen
       
       For i = 1 To Host.hLen
          
          sIPAddr = sIPAddr & tmpIPAddr(i) & "."
       
       Next
       
       GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
       
       SocketsCleanup
        
    End Function
    Public Function GetIPHostName() As String
    
        Dim sHostName As String * 256
        
        If Not SocketsInitialize() Then
            GetIPHostName = ""
            Exit Function
        End If
        
        If gethostname(sHostName, 256) = SOCKET_ERROR Then
            GetIPHostName = "Error in retrieving IP host name!"
            Resume Next
            'MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
                    '" has occurred.  Unable to successfully get Host Name."
            SocketsCleanup
            Exit Function
        End If
        
        GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
        SocketsCleanup
    
    End Function
    Private Function HiByte(ByVal wParam As Integer)
    
        HiByte = wParam \ & H100  And &HFF&
     
    End Function
    Private Function LoByte(ByVal wParam As Integer)
    
        LoByte = wParam And &HFF&
    
    End Function
    Private Sub SocketsCleanup()
    
        If WSACleanup() <> ERROR_SUCCESS Then
            Resume Next
        End If
        
    End Sub
    
    Private Function SocketsInitialize() As Boolean
    
       Dim WSAD As WSADATA
       Dim sLoByte As String
       Dim sHiByte As String
       
       If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
       '"The 32-bit Windows Socket is not responding."
          SocketsInitialize = False
          Exit Function
       End If
       
       
       If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
            'MsgBox "This application requires a minimum of " & _
                    CStr(MIN_SOCKETS_REQD) & " supported sockets."
            
            SocketsInitialize = False
            Exit Function
       End If
       
       
       If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
         (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
          HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
          
          sHiByte = CStr(HiByte(WSAD.wVersion))
          sLoByte = CStr(LoByte(WSAD.wVersion))
          
          'MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
                ' " is not supported by 32-bit Windows Sockets."
          
          SocketsInitialize = False
          Exit Function
          
       End If
        SocketsInitialize = True
    End Function
    
    Property Get GetOnline() As Boolean
        GetOnline = InternetGetConnectedState(0&, 0&)
    End Property
    
    Property Get GetStateOnline() As String
    If GetOnline Then
        GetStateOnline = "Active."
    Else
        GetStateOnline = "Inactive."
    End If
    End Property
    
    Public Function ViaLAN() As Boolean
    
    Dim SFlags As Long
    'return the flags associated with the connection
    Call InternetGetConnectedState(SFlags, 0&)
    
    'True if the Sflags has a LAN connection
    ViaLAN = SFlags And INTERNET_CONNECTION_LAN
    
    End Function
    
    Public Function ViaModem() As Boolean
    
    Dim SFlags As Long
    'return the flags associated with the connection
    Call InternetGetConnectedState(SFlags, 0&)
    
    'True if the Sflags has a modem connection
    ViaModem = SFlags And INTERNET_CONNECTION_MODEM
    
    End Function
    Now, in a form, or a module.
    Code:
    Dim netCls As New Net
    
    'To get the local IP, call this. It returns a string containing the IP of your machine.
    Msgbox netCls.GetIPAddress
    Hope this helps.

    later(z)

    REM
    "Innovate, don't immitate."

  6. #6
    Lively Member Cerebrate's Avatar
    Join Date
    May 2000
    Posts
    82

    Red face I am not sure if this is what your are asking.

    you can get user IP through this call:

    ASP
    ----------------------------------------
    Request. ServerVariables(REMOTE_ADDR)
    ----------------------------------------

    (REMOTE_ADDR is the Internet Protocol (IP) address of the remote host making the request. )


    Cerebrate

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