Page 1 of 2

Mac address how to get it ?

Posted: Mon Feb 28, 2005 3:56 am
by dagcrack
How can I get the mac address of another pc (in lan) with code?.

Posted: Mon Feb 28, 2005 6:52 am
by Beach
I would use WMI to get information from another PC. Here is an example of how to get the MAC address from the local PC using WMI.

Code: Select all

' this should be saved as a VBScript file (filename.vbs)
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
wmisql = "SELECT Description,MACAddress FROM Win32_NetworkAdapterConfiguration"
Set colItems = objWMIService.ExecQuery(wmisql,"WQL")

For Each objItem In colItems
   WScript.Echo "Desc: " & objItem.Description & "  MACAddress: " & objItem.MACAddress
next
It should work on a remote PC but I have not tried it.

Posted: Mon Feb 28, 2005 8:36 am
by griz
You could use Ring's SRGETINFO userlib available at : http://www.purearea.net/pb/english/index.htm

like so :

Code: Select all

debug SRGetMACID(ip$)

Posted: Mon Feb 28, 2005 11:49 am
by ABBKlaus
or you could follow this link viewtopic.php?t=10839&highlight=

Posted: Tue Mar 01, 2005 3:55 am
by Beach
"SEGETINFO" looks like it is for the local PC, the question was how to get the MAC address of 'another' PC. At least that is how I read it.

Posted: Tue Mar 01, 2005 10:11 am
by ABBKlaus
ok then try this out :

Code: Select all

; http://msdn.microsoft.com/library/en-us/iphlp/iphlp/sendarp.asp
;DWORD SendARP(
;  IPAddr DestIP,
;  IPAddr SrcIP,
;  PULONG pMacAddr,
;  PULONG PhyAddrLen
;);
;
Procedure.l GetNetworkComputerIP(computer$)
  ; Originally posted by Hi-Toro
  ; Posted: Sun Feb 16, 2003 8:27 pm
  ; http://forums.purebasic.com/english/viewtopic.php?t=5151
  ; modified on 1.3.2005 by ABBKlaus
  
  If computer$ 
    ; Create WSA version number (damn, ugly!)... 
    high.b = 1: low.b = 1 
    DefType.w wsaversion 
    PokeB (@wsaversion, high) ; Gotta poke major version number into low byte... 
    PokeB (@wsaversion + 1, low) ; ... and minor version number into high byte 
    If WSAStartup_ (wsaversion, wsa.WSAData) = #NOERROR ; Try to access Windows sockets stuff...
      *host.HOSTENTSTRU = gethostbyname_ (computer$) ; Get host information for named computer...
      If *host <> #NULL
        ip = PeekL(PeekL (*host\h_list))
        ips$ = PeekS (inet_ntoa_ (PeekL(PeekL (*host\h_list)))) ; Get IP address of named computer...
      EndIf 
      WSACleanup_ () ; Close Windows sockets stuff...
    EndIf 
    ProcedureReturn ip
  EndIf 
EndProcedure

Procedure.s GetComputerName()
  buffer.s=Space(64)
  bufsize.l=64
  GetComputerName_(@buffer, @bufsize)
  ProcedureReturn buffer
EndProcedure

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

ip=GetNetworkComputerIP("DEM041452") ; Fill in the desired Computername

thisip=GetNetworkComputerIP(GetComputerName())

Debug "This IP="+IPString(thisip)

maclen=6
*buffer=AllocateMemory(8)

If SendARP_(ip,thisip,*buffer,@maclen)=#NO_ERROR
  Debug "SendARP to IP="+IPString(ip)+" Success"
  Debug MacToString(*buffer)
Else
  Debug "SendARP to IP="+IPString(ip)+" Failed"
EndIf

FreeMemory(*buffer)
PS: the GetNetworkComputerIP(computer$) routine is not mine :oops: and i don´t know the author (fixed :P )

Posted: Tue Mar 01, 2005 10:15 am
by traumatic
ABBKlaus wrote: PS: the GetNetworkComputerIP(computer$) routine is not mine :oops: and i don´t know the author
Hi-Toro is the original author AFAIK...
viewtopic.php?t=5151

Posted: Tue Mar 01, 2005 4:45 pm
by Beach
Awesome! Works perfectly here.

Posted: Tue Mar 01, 2005 5:51 pm
by griz
Cool, works good for me too. Thanks.

Posted: Wed Mar 02, 2005 4:23 am
by dagcrack
danke!

Posted: Fri May 19, 2006 10:11 pm
by Psychophanta
ABBKlaus, Could be translated to PB4.0 ??

Posted: Fri May 19, 2006 10:35 pm
by ABBKlaus
Thats easy :D

Code: Select all

; http://msdn.microsoft.com/library/en-us/iphlp/iphlp/sendarp.asp 
;DWORD SendARP( 
;  IPAddr DestIP, 
;  IPAddr SrcIP, 
;  PULONG pMacAddr, 
;  PULONG PhyAddrLen 
;); 
; 
Procedure.l GetNetworkComputerIP(computer$) 
  ; Originally posted by Hi-Toro 
  ; Posted: Sun Feb 16, 2003 8:27 pm 
  ; http://forums.purebasic.com/english/viewtopic.php?t=5151 
  ; modified on 1.3.2005 by ABBKlaus
  ; made PB4.0 compatible on 19.5.2006 by ABBKlaus
  
  If computer$ 
    ; Create WSA version number (damn, ugly!)... 
    high.b = 1: low.b = 1 
    wsaversion.w
    PokeB (@wsaversion, high) ; Gotta poke major version number into low byte... 
    PokeB (@wsaversion + 1, low) ; ... and minor version number into high byte 
    If WSAStartup_ (wsaversion, wsa.WSAData) = #NOERROR ; Try to access Windows sockets stuff... 
      *host.HOSTENT = gethostbyname_ (computer$) ; Get host information for named computer... 
      If *host <> #Null 
        ip = PeekL(PeekL (*host\h_addr_list)) 
        ips$ = PeekS (inet_ntoa_ (PeekL(PeekL (*host\h_addr_list)))) ; Get IP address of named computer... 
      EndIf 
      WSACleanup_ () ; Close Windows sockets stuff... 
    EndIf 
    ProcedureReturn ip 
  EndIf 
EndProcedure 

Procedure.s GetComputerName() 
  buffer.s=Space(64) 
  bufsize.l=64 
  GetComputerName_(@buffer, @bufsize) 
  ProcedureReturn buffer 
EndProcedure 

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

ip=GetNetworkComputerIP("KLAUS-AMD") ; Fill in the desired Computername 

thisip=GetNetworkComputerIP(GetComputerName()) 

Debug "This IP="+IPString(thisip) 

maclen=6 
*buffer=AllocateMemory(8) 

If SendARP_(ip,thisip,*buffer,@maclen)=#NO_ERROR 
  Debug "SendARP to IP="+IPString(ip)+" Success" 
  Debug MacToString(*buffer) 
Else 
  Debug "SendARP to IP="+IPString(ip)+" Failed" 
EndIf 

FreeMemory(*buffer) 

Posted: Fri May 19, 2006 10:37 pm
by Psychophanta
Thanx :D

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