Get own IP Address?
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by jreimers.
Hello PB profs,
any idea about figuring out my own IP-address?
I´ve tried with API Call GetAdaptersInfo_, parameters seems to be correct
(I get description: NDIS 5.0 driver, Adaptername: {xyz...} from reg and the correct MAC).
IP-address is always empty, the link IP_ADAPTER_INFO\next is always NULL.
I´ve tried it also in VB 5.0 with the example in API-Guide vrom KPD (which I also used translated in PB), results are the same!
Of course, I have a NIC and IP installed, OS = W2K prof SP2
Hello PB profs,
any idea about figuring out my own IP-address?
I´ve tried with API Call GetAdaptersInfo_, parameters seems to be correct
(I get description: NDIS 5.0 driver, Adaptername: {xyz...} from reg and the correct MAC).
IP-address is always empty, the link IP_ADAPTER_INFO\next is always NULL.
I´ve tried it also in VB 5.0 with the example in API-Guide vrom KPD (which I also used translated in PB), results are the same!
Of course, I have a NIC and IP installed, OS = W2K prof SP2
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tranquil.
After calling the procedure, all available IPs should written into the array localips()
Hope it helps.
Mike
Tranquilizer/ Secretly!
Registred PureBasic User
Some time ago I posted a little codesnip that collects all IPs of your local system. Its very easy.Hello PB profs,
any idea about figuring out my own IP-address?
I´ve tried with API Call GetAdaptersInfo_, parameters seems to be correct
(I get description: NDIS 5.0 driver, Adaptername: {xyz...} from reg and the correct MAC).
IP-address is always empty, the link IP_ADAPTER_INFO\next is always NULL.
I´ve tried it also in VB 5.0 with the example in API-Guide vrom KPD (which I also used translated in PB), results are the same!
Of course, I have a NIC and IP installed, OS = W2K prof SP2
Code: Select all
; Convert Asigned Word to normal Byte
Procedure.l calc(va.b)
If va.b0
ipb1.b=PeekB(ipadr.l)
ip1.l=calc(ipb1.b)
ipb2.b=PeekB(ipadr.l+1)
ip2.l=calc(ipb2.b)
ipb3.b=PeekB(ipadr.l+2)
ip3.l=calc(ipb3.b)
ipb4.b=PeekB(ipadr.l+3)
ip4.l=calc(ipb4.b)
localips$(ip)=Str(ip1.l)+"."+Str(ip2.l)+"."+Str(ip3.l)+"."+Str(ip4.l)
st$+localips$(ip)+Chr(10):ip+1:offset.l+4
EndIf
Until h_addr_list=0
ProcedureReturn ip
EndProcedure
Hope it helps.
Mike
Tranquilizer/ Secretly!
Registred PureBasic User
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by jreimers.
jreimers
thanks to PB, but it doesn´t, IPAddressGadget() offers only filling in theIP> any idea about figuring out my own IP-address?
I haven't tried this myself, but v3.10 has an IPAddressGadget() which probably
does what you want...
PB - Registered PureBasic Coder
jreimers
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by jreimers.
Thanks to tranquil,
this seems to make sense, but because Your code only returns empty strings (I think because winsock isn´t initialized and there are no error traps).
Do You have annother code snippet including the winsock initialisation?
Would be nice!
Thanks to tranquil,
this seems to make sense, but because Your code only returns empty strings (I think because winsock isn´t initialized and there are no error traps).
Do You have annother code snippet including the winsock initialisation?
Would be nice!
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tranquil.
I'm now using Windows 2000 2 LAN Cards, ISDN Card and Internet.
I get all IPs of my local system with the above procedure correctly. Hm...
Mike
Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
I'm now using Windows 2000 2 LAN Cards, ISDN Card and Internet.
I get all IPs of my local system with the above procedure correctly. Hm...
Mike
Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
I add the InitNetwork()line at the begginig and the rest of the code as is and nothing, only void strings.
If i check what is happenning i saw that:
adr.l=gethostbyname_(*buffer) get a value
h_name.l=PeekL(adr.l) ;Point to host name get a value
ip=0:offset.l=0
Repeat
h_addr_list=PeekL(adr.l+16+offset.l) ; List of Addresses (IPs) void
and the ofcourse it skip this part (since h_addr_list = 0)
If h_addr_list0
...
thats why im getting no results.
But what can i doing wrong?I'm now using Windows 2000 2 LAN Cards, ISDN Card and Internet.
I get all IPs of my local system with the above procedure correctly. Hm...
I add the InitNetwork()line at the begginig and the rest of the code as is and nothing, only void strings.
If i check what is happenning i saw that:
adr.l=gethostbyname_(*buffer) get a value
h_name.l=PeekL(adr.l) ;Point to host name get a value
ip=0:offset.l=0
Repeat
h_addr_list=PeekL(adr.l+16+offset.l) ; List of Addresses (IPs) void
and the ofcourse it skip this part (since h_addr_list = 0)
If h_addr_list0
...
thats why im getting no results.
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
Add some error checking and try again.
Maybe InitNetwork() fails for you...
cya,
...Danilo
(registered PureBasic user)
Add some error checking and try again.
Maybe InitNetwork() fails for you...
Code: Select all
Dim localips$(10)
Procedure.l calc(va.b)
; Convert Asigned Word to normal Byte
If va.b0
ipb1.b=PeekB(ipadr.l)
ip1.l=calc(ipb1.b)
ipb2.b=PeekB(ipadr.l+1)
ip2.l=calc(ipb2.b)
ipb3.b=PeekB(ipadr.l+2)
ip3.l=calc(ipb3.b)
ipb4.b=PeekB(ipadr.l+3)
ip4.l=calc(ipb4.b)
localips$(ip)=Str(ip1.l)+"."+Str(ip2.l)+"."+Str(ip3.l)+"."+Str(ip4.l)
st$+localips$(ip)+Chr(10):ip+1:offset.l+4
EndIf
Until h_addr_list=0
ProcedureReturn ip
EndProcedure
If InitNetwork()
IPcount = getIPs()
If IPcount
A$ = "IP´s available: "+Str(IPcount)+Chr(13)+Chr(13)
For a = 0 To IPcount - 1
A$ + "IP " + Str(a) + " : " + localips$(a) + Chr(13)
Next a
Else
A$ = "No IP´s found"
EndIf
MessageRequester("IP INFO",A$,0)
Else
MessageRequester("ERROR","Cant initialize Network!",0)
EndIf
cya,
...Danilo
(registered PureBasic user)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
@Tranquil:
The line " a=gethostname_(*buffer,2048) " in your
procedure getIPs() is useless (not needed).
Same with " st$+localIPs$(ip)+Chr(10) " - dead code.
Your procedure calc() isnt needed here.
We have StrU() for that in PureBasic.
So the 8 variables ip1, ip2, ip3, ip4, ipb1, ipb2, ipb3, ipb4
are not needed anymore.
Maybe you want to change it, so you can get the
Hostname in localIPs$(0) and all IPs after that:
Or just remove this useless lines...
cya,
...Danilo
(registered PureBasic user)
Edited by - Danilo on 16 August 2002 19:21:43
@Tranquil:
The line " a=gethostname_(*buffer,2048) " in your
procedure getIPs() is useless (not needed).
Same with " st$+localIPs$(ip)+Chr(10) " - dead code.
Your procedure calc() isnt needed here.
We have StrU() for that in PureBasic.
So the 8 variables ip1, ip2, ip3, ip4, ipb1, ipb2, ipb3, ipb4
are not needed anymore.
Maybe you want to change it, so you can get the
Hostname in localIPs$(0) and all IPs after that:
Code: Select all
IPmax = 10
Dim localips$(IPmax)
Procedure.l getIPs()
Shared localIPs$,IPmax
*buffer = GlobalAlloc_(#GMEM_FIXED | #GMEM_ZEROINIT, 65536)
Dim localIPs$(IPmax) ; Remove all existent IPs from list ( REDIM = Clear )
a=gethostname_(*buffer,2048)
localIPs$(0) = UCase(PeekS(*buffer))
adr.l=gethostbyname_(*buffer)
h_name.l=PeekL(adr.l) ;Point to host name
ip=0:offset.l=0
Repeat
h_addr_list=PeekL(adr.l+16+offset.l) ; List of Addresses (IPs)
ipadr.l=h_addr_list
If h_addr_list0
localIPs$(ip+1)=StrU(PeekB(ipadr.l),0)+"."+StrU(PeekB(ipadr.l+1),0)+"."+StrU(PeekB(ipadr.l+2),0)+"."+StrU(PeekB(ipadr.l+3),0)
;st$+localIPs$(ip)+Chr(10)
ip+1:offset.l+4
EndIf
Until h_addr_list=0
ProcedureReturn ip
EndProcedure
If InitNetwork()
IPcount = getIPs()
If IPcount
A$ = "IP´s available on: "+Chr(13)+Chr(13)+Chr(34)+localIPs$(0)+Chr(34)+Chr(13)+Chr(13)
For a = 1 To IPcount
A$ + "IP " + Str(a) + " : " + localIPs$(a) + Chr(13)
Next a
Else
A$ = "No IP´s found"
EndIf
MessageRequester("IP INFO",A$,0)
Else
MessageRequester("ERROR","Cant initialize Network!",0)
EndIf
Or just remove this useless lines...
cya,
...Danilo
(registered PureBasic user)
Edited by - Danilo on 16 August 2002 19:21:43
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Franco.
Sorry Danilo, but your code doesn't work on my machine (Win2000-Sp3).
The last 3 digits are:
4294967214
instead of:
174
This is because you have coded:
localIPs$(ip+1)=StrU(PeekB(ipadr.l),0)+"."+StrU(PeekB(ipadr.l+1),0)+"."+StrU(PeekB(ipadr.l+2),0)+"."+StrU(PeekB(ipadr.l+3),2)
instead of:
localIPs$(ip+1)=StrU(PeekB(ipadr.l),0)+"."+StrU(PeekB(ipadr.l+1),0)+"."+StrU(PeekB(ipadr.l+2),0)+"."+StrU(PeekB(ipadr.l+3),0)
Have a nice day...
Franco
Sometimes you have to go a lonely way to accomplish genius things.
Sorry Danilo, but your code doesn't work on my machine (Win2000-Sp3).
The last 3 digits are:
4294967214
instead of:
174
This is because you have coded:
localIPs$(ip+1)=StrU(PeekB(ipadr.l),0)+"."+StrU(PeekB(ipadr.l+1),0)+"."+StrU(PeekB(ipadr.l+2),0)+"."+StrU(PeekB(ipadr.l+3),2)
instead of:
localIPs$(ip+1)=StrU(PeekB(ipadr.l),0)+"."+StrU(PeekB(ipadr.l+1),0)+"."+StrU(PeekB(ipadr.l+2),0)+"."+StrU(PeekB(ipadr.l+3),0)
Have a nice day...
Franco
Sometimes you have to go a lonely way to accomplish genius things.
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tranquil.
@Danielo:
I know that Calc() isn't needed any more but as I coded this procedure strU did not exist in the PB language.
Cheers
Mike
Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
@Danielo:
I know that Calc() isn't needed any more but as I coded this procedure strU did not exist in the PB language.

Cheers
Mike
Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm