
Mac address how to get it ?
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
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.

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.

No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Re: Mac address how to get it ?
does this even work in PB4.4 ?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
-
- Enthusiast
- Posts: 796
- Joined: Tue May 20, 2008 2:12 am
- Location: Cologne, Germany
- Contact:
Re: Mac address how to get it ?
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.
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.
Regards,
JamiroKwai
JamiroKwai
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Mac address how to get it ?
Does not work with PB4.51 RC1 (x86) on WinXP 32bit:
Syntax error.
This does work on WinXP 32bit:
http://www.purebasic.fr/english/viewtop ... 0&start=30
Code: Select all
Procedure.l GetIPAddresses(usrList.ADDRESS(), HostName.s = "")
This does work on WinXP 32bit:
http://www.purebasic.fr/english/viewtop ... 0&start=30
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- 666
- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
Re: Mac address how to get it ?
try instead.
oh, and while I remember... change to
Code: Select all
Procedure.l GetIPAddresses(List usrList.ADDRESS(), HostName.s = "")
oh, and while I remember... change
Code: Select all
ProcedureReturn CountList(usrList())
Code: Select all
ProcedureReturn ListSize(usrList())
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Mac address how to get it ?
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.
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.

Last edited by IdeasVacuum on Fri Nov 19, 2010 2:23 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Re: Mac address how to get it ?
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?

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 ?
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.
I also think there is a function called HostName()... 
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

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: Mac address how to get it ?
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()