• Welcome to Valhalla Legends Archive.
 

API to get local IP?

Started by GSX, September 21, 2006, 07:01 PM

Previous topic - Next topic

GSX

Errr, I need to know it and how to implement it.

I'm pretty rusty at Visual Basic, which is why I came here to ask, seeing as I haven't programmed for about 18 months.

Sorry that this is such a rediculous question, but any help would be appreciated.

Thanks.

RealityRipple

two ways i know of:
1) Put a Microsoft Winsock control on your form and use Winsock1.LocalIP. Note that this will return the first IP found (usually the LAN IP if you have an NIC card installed), so it's not always accurate.

2) Use Microsoft Internet Transfer Control to read a PHP page that displays your IP. Something like this: Inet1.OpenURL("http://rcb.realityripple.com/ip.php"). This will get your IP as viewed from the outside world, so if you're behind a router, it'll display the router's IP. Note that the above URL does work, but I don't promise it always will work. You may want to use another site and parse the IP from the HTML code returned.

GSX


l2k-Shadow

But if you don't want to use controls, here is the API way:



' GetLocalIP() by l2k-Shadow

Option Base 0
Option Explicit
Option Compare Binary

Private Declare Sub CopyMemorylng Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByVal Source As Long, ByVal numbytes As Long)
Private Declare Function WSAStartup Lib "ws2_32" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Private Declare Function WSACleanup Lib "ws2_32" () As Long
Private Declare Function gethostname Lib "ws2_32" (ByVal shost As String, ByVal hostlen As Long) As Long
Private Declare Function gethostbyname Lib "ws2_32" (ByVal host_name As String) As Long

Private Type WSADataType
    wVersion As Integer
    wHighVersion As Integer
    szDescription As String * 256
    szSystemStatus As String * 128
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpVendorInfo As Long
End Type
Private Type HostEnt
    h_name As Long
    h_aliases As Long
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long
End Type

Public Function GetLocalIP() As String
Dim WSA As WSADataType, HostName As String, lHost As Long, hEnt As HostEnt, lIP As Long, sIP(3) As Byte, i As Integer

    If WSAStartup(&H101, WSA) = -1 Then
        Call MsgBox("WSAStartup() Error")
        Call WSACleanup
        Exit Function
    End If
   
    HostName = String$(256, vbNullChar)
   
    If gethostname(HostName, 256) = -1 Then
        Call MsgBox("gethostname() Error")
        Call WSACleanup
        Exit Function
    End If
   
    HostName = Trim$(HostName)
   
    lHost = gethostbyname(HostName)
    If lHost = 0 Then
        Call MsgBox("gethostbyname() Error")
        Call WSACleanup
        Exit Function
    End If

    Call WSACleanup
   
    Call CopyMemorylng(hEnt, lHost, Len(hEnt))
    Call CopyMemorylng(lIP, hEnt.h_addr_list, 4)
    Call CopyMemorylng(sIP(0), lIP, hEnt.h_length)
    For i = 0 To 3
        GetLocalIP = GetLocalIP & sIP(i) & "."
    Next i
   
    GetLocalIP = Left$(GetLocalIP, Len(GetLocalIP) - 1)
End Function
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

GSX

Sweet, just what I was looking for. Thanks l2-Shadow and RealityRipple for all the help.

Cat Food

I would go with the website/php method. You can grab the REAL ip with about 6 lines of code and no controls.

l2k-Shadow

#6
Quote from: ImaWh0re on September 22, 2006, 12:19 AM
I would go with the website/php method. You can grab the REAL ip with about 6 lines of code and no controls.

He's asking for local IP, in case you didn't notice, and FYI that code has to use the Inet or Winsock control.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

RealityRipple

therein lies the problem. I have a modem and a network card. My IP for my local network is 1.1.1.100. My IP for my modem, and thus for all internet-based things, is 67.150.12.193 (for now, since it's dynamic). The Website method will get the one used for internet-based things, and the winsock one will get the first one it finds.

Cat Food

If he needs to get a local IP than more than likely his form will contain a winsock/inet control. Why would you need a local IP so badly if you wern't accessing the net? Yeah I'm sure there are some cases but still....


Inet1.OpenUrl("http://whatismyip.org")
'If using inet


or..


Private Sub Form_Load()
 
  ws1.Connect "www.whatismyip.org", 80
 
End Sub

Private Sub ws1_Connect()
 
  Dim sSend As String
  sSend = "GET / HTTP/1.1" & vbCrLf
  sSend = sSend & "Host: www.whatismyip.org" & vbCrLf
  sSend = sSend & "User-Agent: FF>IE" & vbCrLf & vbCrLf
 
  ws1.SendData sSend
 
End Sub

Private Sub ws1_DataArrival(ByVal bytesTotal As Long)
 
  Dim sData As String
  ws1.GetData sData
 
  If InStrB(1, sData, vbCrLf & vbCrLf) Then MsgBox "My REAL IP is: " & Mid$(sData, InStr(1, sData, vbCrLf & vbCrLf) + 4)

End Sub


--edited for code tags--




(Would of replied quicker but whatismyip.org limits connections in x time when I was packetlogging)

PS: l2k-shadow, don't put "' GetLocalIP() by l2k-Shadow" when infact it is coding from api-guide with renamed variables. K Thanks!

RealityRipple


topaz

#10
[shameless plug]

"""Python"""

import socket

def get_local_addr():
    hostname = socket.gethostname()
    return socket.gethostbyname(hostname)


Edit:

For multiple network interfaces...



def get_local_addrs()
    hostname = socket.gethostname()
    return socket.gethostbyname_ex(hostname)[2]


Ten times less code.
RLY...?

FrOzeN

Quote from: Topaz on September 22, 2006, 01:45 AM
Ten times less code.
Yeah, too bad it's the Visual Basic forum.
~ FrOzeN

topaz

Quote from: FrOzeN on September 22, 2006, 02:05 AM
Quote from: Topaz on September 22, 2006, 01:45 AM
Ten times less code.
Yeah, too bad it's the Visual Basic forum.

Why, are you jealous?

I would be.
RLY...?

Cat Food

Off topic. Thread has been answered :)
PS: GSX -- For tons of useful api and example code API-Guide is a MUST among vb coders. Just don't take snippets like some people above and slap your name all over it.
Official Site: http://www.allapi.net/agnet/apiguide.shtml

l2k-Shadow

Quote from: ImaWh0re on September 22, 2006, 12:15 PM
Off topic. Thread has been answered :)
PS: GSX -- For tons of useful api and example code API-Guide is a MUST among vb coders. Just don't take snippets like some people above and slap your name all over it.
Official Site: http://www.allapi.net/agnet/apiguide.shtml

Fleet-/Fleet-'s dick sucker, you are an idiot. This code is from my winsock module from over a year ago, the declerations are taken from public documentation on msdn//pscode. How can you even say a sub that calls 5 APIs is ripped is beyond me, but then again you would be lucky if your brain was half the size of my right nut, so it's understandable.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.