Page 2 of 2
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?

Re: Mac address how to get it ?
Posted: Fri Nov 19, 2010 12:07 am
by blueznl
Browsing back I've done some similar stuff when doing WinSock, but that wasn't unicode either... Here's a quick go at turning Flype's code into Unicode compatible stuff. I probably should have used prototypes / pseudotypes, I guess, but this seems to work as well.
Code: Select all
Procedure.s x_getipaddress(host.s) ; find ip address for given hostname
Protected *host.HOSTENT, *m
;
; based on code by flype
;
If host > ""
If WSAStartup_ ((1<<8|1), wsa.WSADATA) = #NOERROR
*m = AllocateMemory(#MAX_COMPUTERNAME_LENGTH+1)
PokeS(*m,host,#MAX_COMPUTERNAME_LENGTH,#PB_Ascii)
*host.HOSTENT = gethostbyname_(*m)
WSACleanup_()
FreeMemory(*m)
If *host
ProcedureReturn PeekS(inet_ntoa_(PeekL(PeekL(*host\h_addr_list))),#MAX_COMPUTERNAME_LENGTH,#PB_Ascii)
EndIf
EndIf
EndIf
EndProcedure
Procedure.s x_getmacaddress(ip.s) ; find mac address for given ip address
Protected n.i, l.i, *ip, *mac, mac.s
;
; based on code by flype
;
l = Len(ip)
n = 6
;
*ip = AllocateMemory(l+1)
*mac = AllocateMemory(n)
PokeS(*ip,ip,l,#PB_Ascii)
If SendARP_(inet_addr_(*ip),0,*mac, @n) = #NO_ERROR
For i = 0 To n - 2
mac = mac + RSet(Hex(PeekA(*mac+i)),2,"0") + ":"
Next
mac = mac + RSet(Hex(PeekA(*mac+i)),2,"0")
Else
mac = ""
EndIf
FreeMemory(*ip)
FreeMemory(*mac)
ProcedureReturn mac
EndProcedure
I also think there is a function called HostName()...

Re: Mac address how to get it ?
Posted: Sat Feb 02, 2019 6:16 am
by Opcode
Some of the solutions here worked (partially), however I needed something fast and with PB5 there's a speedy way to pull a mac address.
Code: Select all
InitNetwork()
Procedure GetIPAddr()
If ExamineIPAddresses()
IP = NextIPAddress()
If IP
ProcedureReturn IP
EndIf
EndIf
EndProcedure
Procedure.s GetMacAddr()
Protected.l n = 6
Dim MacAddr.b(n)
If SendARP_(GetIPAddr(), 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
Debug Hostname()
Debug IPString(GetIPAddr())
Debug GetMacAddr()