Page 1 of 2
Posted: Sat May 20, 2006 12:49 am
by Flype
a variant :
Code: Select all
Procedure.s GetIpAddr(ComputerName.s)
If ComputerName
If WSAStartup_ ((1<<8|1), wsa.WSADATA) = #NOERROR
*host.HOSTENT = gethostbyname_(ComputerName)
WSACleanup_()
If *host
ProcedureReturn PeekS(inet_ntoa_(PeekL(PeekL(*host\h_addr_list))))
EndIf
EndIf
EndIf
EndProcedure
Procedure.s GetMacAddr(IP.s)
n.l = 6
Dim MacAddr.b(n)
If SendARP_(inet_addr_(IP), 0, @MacAddr(0), @n) = #NO_ERROR
For i = 0 To n - 2
MAC$ + RSet(Hex(MacAddr(i)&255),2,"0") + ":"
Next
ProcedureReturn MAC$ + RSet(Hex(MacAddr(i)&255),2,"0")
EndIf
EndProcedure
Procedure.s GetHostName()
Size.l = 32
Host.s = Space(Size)
GetComputerName_(@Host, @Size)
ProcedureReturn Host
EndProcedure
Debug GetHostName()
Debug GetIpAddr(GetHostName())
Debug GetMacAddr(GetIpAddr(GetHostName()))
Posted: Sat May 20, 2006 1:48 am
by rsts
Very nice Flype.
cheers
Posted: Sat May 20, 2006 10:56 am
by Psychophanta
Thank you Flype

Posted: Sat May 20, 2006 11:59 am
by ABBKlaus
Fantastic

and cleaner than mine

Posted: Sat May 20, 2006 3:13 pm
by Flype
feel the PB4 power
Code: Select all
Structure ADDRESS
IpLong.l
IpString.s
MacString.s
MacLength.l
MacArray.b[6]
EndStructure
Procedure.l GetIPAddresses(usrList.ADDRESS(), HostName.s = "")
Protected wsa.WSADATA, *host.HOSTENT
If WSAStartup_(1<<8|1, wsa) = #NOERROR
*host = gethostbyname_(HostName)
WSACleanup_()
If *host
For i = 0 To *host\h_length - 1
If AddElement(usrList())
usrList()\IpLong = PeekL(PeekL(*host\h_addr_list)+(i*SizeOf(Long)))
usrList()\IpString = IPString(usrList()\IpLong)
usrList()\MacLength = SizeOf(ADDRESS\MacArray)
If SendARP_(usrList()\IpLong, 0, @usrList()\MacArray, @usrList()\MacLength) = #NO_ERROR
For j = 0 To usrList()\MacLength - 2
usrList()\MacString + RSet(Hex(usrList()\MacArray[j] & 255), 2, "0") + ":"
Next
usrList()\MacString + RSet(Hex(usrList()\MacArray[j] & 255), 2, "0")
EndIf
EndIf
Next
EndIf
EndIf
ProcedureReturn CountList(usrList())
EndProcedure
NewList list.ADDRESS()
If GetIPAddresses(list())
ForEach list()
Debug "IP: " + list()\IpString
Debug "MAC: " + list()\MacString
Next
EndIf
Posted: Sat May 20, 2006 7:40 pm
by netmaestro
Nice code, Flype! If I may make one infinitely tiny suggestion, PB4 has unsigned bytes now, so MacArray.c[6] would be slightly cleaner as & 255 wouldn't be needed.
Posted: Sat May 20, 2006 7:48 pm
by Psychophanta
netmaestro wrote:Nice code, Flype! If I may make one infinitely tiny suggestion, PB4 has unsigned bytes now, so MacArray.c[6] would be slightly cleaner as & 255 wouldn't be needed.
Indeed, that's a point

Posted: Sat May 20, 2006 8:59 pm
by Flype
yes but no
if you do that it becomes no more compatible with unicode compiler option.
so it's better to let .b because .c is not a true unsigned byte as it is 2 bytes length in unicode.

Re: Mac address how to get it ?
Posted: Fri Dec 11, 2009 1:59 pm
by Haruks
Thank you Flype!!!
Re: Mac address how to get it ?
Posted: Sat Dec 12, 2009 10:54 am
by SFSxOI
does this even work in PB4.4 ?
Re: Mac address how to get it ?
Posted: Sat Dec 12, 2009 11:08 am
by jamirokwai
Hi,
has anyone tried to implement this for Linux or Mac? I'd be heavily interested...
I am also seeking for a cross-platform-way to find all computers in a network... Like Apples Bonjour.
Re: Mac address how to get it ?
Posted: Thu Nov 18, 2010 7:09 pm
by IdeasVacuum
Does not work with PB4.51 RC1 (x86) on WinXP 32bit:
Code: Select all
Procedure.l GetIPAddresses(usrList.ADDRESS(), HostName.s = "")
Syntax error.
This does work on WinXP 32bit:
http://www.purebasic.fr/english/viewtop ... 0&start=30
Re: Mac address how to get it ?
Posted: Thu Nov 18, 2010 7:25 pm
by LuCiFeR[SD]
try
Code: Select all
Procedure.l GetIPAddresses(List usrList.ADDRESS(), HostName.s = "")
instead.
oh, and while I remember... change
Code: Select all
ProcedureReturn CountList(usrList())
to
Code: Select all
ProcedureReturn ListSize(usrList())
Re: Mac address how to get it ?
Posted: Thu Nov 18, 2010 7:39 pm
by IdeasVacuum
Thank you, that does make it work - I noticed the CountList depreciation.
However, after all, the code is not suitable because it only finds the MAC address if the NIC is connected
Edit: I should say, not suitable for returning the address of the PC it is run from, since I wanted the code for that purpose alone, which in fact it was not intended for anyway.

Re: Mac address how to get it ?
Posted: Thu Nov 18, 2010 8:28 pm
by Rook Zimbabwe
Nice one Flype!
Though the MAC ADD can be spoofed easily so if you aare planning on crunking someone on a LAN party... does this work too?
