Mac address how to get it ?

Just starting out? Need help? Post your questions and find answers here.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Mac address how to get it ?

Post by dagcrack »

How can I get the mac address of another pc (in lan) with code?.
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post 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.
-Beach
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post 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$)
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

or you could follow this link viewtopic.php?t=10839&highlight=
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post 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.
-Beach
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post 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 )
Last edited by ABBKlaus on Tue Mar 01, 2005 10:21 am, edited 1 time in total.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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
Good programmers don't comment their code. It was hard to write, should be hard to read.
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Awesome! Works perfectly here.
-Beach
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

Cool, works good for me too. Thanks.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

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

Post by Psychophanta »

ABBKlaus, Could be translated to PB4.0 ??
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 »

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

Post by Psychophanta »

Thanx :D
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 »

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