Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mac Address

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi,

Please refer to the thread184-756938. Kindly let me know how to get only highlighted mac address.

Thanks

Saif
macerr_bfgohy.png
 
You don't seem to see the woods for the trees.

Extract the code about MAC addresses:

Code:
CLEAR

*!* Let's get the MAC Address(es)
LOCAL lcComputerName, loWMIService, loItems, loItem, lcMACAddress
lcComputerName = "."
loWMIService = GETOBJECT("winmgmts:\\" + lcComputerName + "\root\cimv2")
loItems = loWMIService.ExecQuery("Select * from Win32_NetworkAdapter [highlight #FCE94F]Where PhysicalAdapter=1[/highlight]",,48)
FOR EACH loItem IN loItems
    lcMACAddress = loItem.MACAddress
    IF !ISNULL(lcMACAddress)
        ?  [highlight #FCE94F]loItem.Name[/highlight]+" MAC Address: " + loItem.MACAddress
        ?
    ENDIF
ENDFOR
I just added the Name and PhysicalAdapter=1 filter, so you see what MAC address is of real devices. You shouldn't use MACs of virtual devices.

If you have keywords like Win32_NetworkAdapter, please simply google, alisaif:

This lists all further properties of the class. So you can see, what you have at hand and how you can filter and what other infos you can retrieve.

That said, I don't know if this will give you the MAC ID you highlight in your screenshot. It doesn't tell of which device this is the MAC. I just assume you want a physical device MAC address. Be warned though, not only virtual device MAC addresses can be changed.

Bye, Olaf.
 
Thanks I use the following code in order to obtain IP and Mac Address:
Code:
CLEAR 
lcComputer      = [.]
loWMIService   = GETOBJECT( [winmgmts:\\] + lcComputer + [\root\cimv2] )
loIPConfigSet   = loWMIService.ExecQuery( [Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True] )

FOR EACH loIPConfig IN loIPConfigSet
   IF !ISNULL( loIPConfig.IPAddress )
      ? [IP Address is  : ] + loIPConfig.IPAddress( 0 )
      ? [Mac address is : ] + IpToMacAddress( loIPConfig.IPAddress( 0 ) )
   ENDIF 
ENDFOR 


FUNCTION IpToMacAddress
LPARAMETERS vIP
        DECLARE INTEGER inet_addr IN ws2_32.dll STRING cIP
        DECLARE INTEGER SendARP IN iphlpapi.dll;
                INTEGER destIP, INTEGER sourceIP,;
                STRING @ pMacAddr, INTEGER @ PhyAddrLen
        LOCAL lnHr, lnIpAddr, lcMacAddr, lnLen
        lnIpAddr   = inet_addr( vIP )
        lcMacAddr   = REPLICATE( CHR( 0 ) , 6 )
        lnLen      = 6
        lnHr      = SendARP( lnIpAddr , 0 , @lcMacAddr , @lnLen )
        RETURN BinaryToMac( lcMacAddr , lnLen )
ENDFUNC

FUNCTION BinaryToMac
LPARAMETERS vMacAddr , vLen

   LOCAL lcMac, xj
   lcMac = []
   FOR xj = 1 TO vLen - 1
      lcMac = lcMac + RIGHT( TRANSFORM( ASC( SUBSTR( vMacAddr , xj , 1 ) ) , [@0] ) , 2 ) + [:]
   ENDFOR
   lcMac = lcMac + RIGHT( TRANSFORM( ASC( SUBSTR( vMacAddr , vLen , 1 ) ) , [@0] ) , 2 )
   RETURN lcMac
   
ENDFUNC
Saif
 
Is that code solving your question? Or is it the code giving you two MAC addresses and troubling you? We can't read your mind, please be more verbose.

On my home computer your code only retrieves 1 IP and MAC address, but IPEnabled can be true for physical and virtual networkadapters, especially with hosting virtual machines your host system will emulate hardware and have virtual network cards for both the host system and the virtual machines to emulate a network. If you don't want those MAC addresses, you have to limit the result further and Win32_NetworkAdapter seems the better choice for that to me, filtered for PhysicalAdapter=1.

You query another class, Win32_NetworkAdapterConfiguration ( that's surely connected to Win32_NetworkAdapter, i.e. via the Index.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top