Get MAC Adress from IP (Windows only)

Share your advanced PureBasic knowledge/code with the community.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Get MAC Adress from IP (Windows only)

Post by ABBKlaus »

tested with PB4.10 on Windows XP / Vista Home Basic (32-Bit)

Code: Select all

EnableExplicit

InitNetwork()

Procedure.l GetNetworkComputerIP(computer$) ; returns ip adress of hostname/IP$
  ; Originally posted by Hi-Toro
  ; Posted: Sun Feb 16, 2003 8:27 pm
  ; http://purebasicforums.com/english/viewtopic.php?t=5151
  ; modified on 1.3.2005 by ABBKlaus
  ; modified on 8.12.2007 by ABBKlaus (unicode compatible)
  Protected *Buffer,*host.HOSTENT,ip.l
  
  If computer$ 
    *Buffer=AllocateMemory(MemoryStringLength(@computer$)+1)
    If *Buffer
      PokeS(*Buffer,computer$,-1,#PB_Ascii)
      *host = gethostbyname_(*Buffer) ; Get host information for named computer...
      If *host
        ip = PeekL(PeekL(*host\h_addr_list))
      EndIf 
      FreeMemory(*Buffer)
    EndIf 
  EndIf
  
  ProcedureReturn ip
EndProcedure

Procedure.s MacToString(*membuffer) ; returns MAC adress in string format
  Protected MAC.s,i.l
  
  MAC = ""
  For i=0 To 5
    MAC+RSet(Hex(PeekB(*membuffer+i)&$FF),2,"0")
    If i<5
      MAC+":"
    EndIf
  Next
  
  ProcedureReturn MAC
EndProcedure

Procedure.s GetMacFromIP(IP$) ; returns MAC adress from hostname or IP
  ;ABBKlaus on 7.12.2007
  ;http://msdn.microsoft.com/library/en-us/iphlp/iphlp/sendarp.asp
  Protected ip.l,thisip.l,maclen.l,*buffer,mac.s=""
  
  ip=GetNetworkComputerIP(IP$)
  thisip=GetNetworkComputerIP(Hostname())
  
  maclen=6
  *buffer=AllocateMemory(8)
  If *buffer
    If SendARP_(ip,thisip,*buffer,@maclen)=#NO_ERROR
      mac=MacToString(*buffer)
    Else
      Debug "SendARP failed"
    EndIf
    FreeMemory(*buffer)
  EndIf
  
  ProcedureReturn mac
EndProcedure

Debug GetMacFromIP("192.168.1.110") ; Fill in the desired Computername
Debug GetMacFromIP("klaus-core2") ; Fill in the desired Computername
Last edited by ABBKlaus on Sat Dec 08, 2007 12:51 pm, edited 2 times in total.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Does this work on Vista? I get ARP Failed.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Very nice - works fine here under Vista.

Good tip.

cheers
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Post by Hi-Toro »

Nice one, Klaus, fine on XP here.

SFSxOI, are you changing the IP address passed to the function?

Code: Select all

Debug GetMacFromIP("klaus-core2") ' Insert own IP/PC name here!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

should work on vista, and now in unicode mode too :wink:
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Thanks this is very handy indeed.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

got it working now, was a stupid permission thing.

Nice job. Thank You :)

Hi-Toro wrote:Nice one, Klaus, fine on XP here.

SFSxOI, are you changing the IP address passed to the function?

Code: Select all

Debug GetMacFromIP("klaus-core2") ' Insert own IP/PC name here!
Post Reply