Mac address how to get it ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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())) 
Last edited by Flype on Sat May 20, 2006 12:06 pm, edited 1 time in total.
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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Very nice Flype.

cheers
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thank you Flype 8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

Fantastic :D and cleaner than mine :oops:
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

feel the PB4 power :P

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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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.

:wink:
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
User avatar
Haruks
User
User
Posts: 30
Joined: Wed Sep 23, 2009 1:41 am
Location: Brasil (Brazil)

Re: Mac address how to get it ?

Post by Haruks »

Thank you Flype!!!
=D
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Mac address how to get it ?

Post by SFSxOI »

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.
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Mac address how to get it ?

Post by jamirokwai »

Hi,

has anyone tried to implement this for Linux or Mac? I'd be heavily interested... :P
I am also seeking for a cross-platform-way to find all computers in a network... Like Apples Bonjour.
Regards,
JamiroKwai
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Mac address how to get it ?

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Mac address how to get it ?

Post 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())
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Mac address how to get it ?

Post 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. :oops:
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.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Mac address how to get it ?

Post by Rook Zimbabwe »

Nice one Flype! 8)

Though the MAC ADD can be spoofed easily so if you aare planning on crunking someone on a LAN party... does this work too? :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply