AfxGetMACAddress

Started by Rudolf Furstauer, August 23, 2012, 03:13:53 PM

Previous topic - Next topic

Rudolf Furstauer

This funktion only works if only one network card is active.
On my pc I get the following information:

Y:\>getmac

Physikal. Adresse   Transportname
=================== ==========================================================
02-93-30-5F-54-C1   \Device\Tcpip_{B2FE83AC-97DA-4578-92C1-84F75CD9CA2C}
9C-8E-99-4C-D9-8C   \Device\Tcpip_{54683DA7-6950-4217-8632-389B56DB8641}
08-00-27-00-68-94   \Device\Tcpip_{D81C03B0-1E26-4B85-97E4-E5F1D164D24E}


The result of AfxGetMACAddress is: '08-00-27-00-68-94',
but the mac-address of the lan-card is: '9C-8E-99-4C-D9-8C'

Is there a way to list all the mac-addresses of the computer
and find out what is what.


Thanks for any information.



Rudolf Fürstauer

José Roca

Yes.


#COMPILE EXE
#DIM ALL
#INCLUDE "WMI.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL hr AS LONG                                  ' // HRESULT
   LOCAL pService AS ISWbemServices                  ' // Services object
   LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
   LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
   LOCAL bstrDisplayName AS WSTRING                  ' // Display name
   LOCAL bstrQuery AS WSTRING                        ' // Query string
   LOCAL oItem AS DISPATCH                           ' // Generic object variable
   LOCAL vItem AS VARIANT                            ' // Generic object variant
   LOCAL vRes AS VARIANT                             ' // General purpose variant

   ' // Variants to store the property values
   LOCAL vAdapterType AS VARIANT                     ' // String
   LOCAL vAdapterTypeId AS VARIANT                   ' // Unsigned 16-bit integer
   LOCAL vAutoSense AS VARIANT                       ' // Boolean value
   LOCAL vAvailability AS VARIANT                    ' // Unsigned 16-bit integer
   LOCAL vCaption AS VARIANT                         ' // String
   LOCAL vConfigManagerErrorCode AS VARIANT          ' // Unsigned 32-bit integer
   LOCAL vConfigManagerUserConfig AS VARIANT         ' // Boolean value
   LOCAL vCreationClassName AS VARIANT               ' // String
   LOCAL vDescription AS VARIANT                     ' // String
   LOCAL vDeviceID AS VARIANT                        ' // String
   LOCAL vErrorCleared AS VARIANT                    ' // Boolean value
   LOCAL vErrorDescription AS VARIANT                ' // String
   LOCAL vGUID AS VARIANT                            ' // String
   LOCAL vIndex AS VARIANT                           ' // Unsigned 32-bit integer
   LOCAL vInstallDate AS VARIANT                     ' // Date/time value
   LOCAL vInstalled AS VARIANT                       ' // Boolean value
   LOCAL vInterfaceIndex AS VARIANT                  ' // Unsigned 32-bit integer
   LOCAL vLastErrorCode AS VARIANT                   ' // Unsigned 32-bit integer
   LOCAL vMACAddress AS VARIANT                      ' // String
   LOCAL vManufacturer AS VARIANT                    ' // String
   LOCAL vMaxNumberControlled AS VARIANT             ' // Unsigned 32-bit integer
   LOCAL vMaxSpeed AS VARIANT                        ' // Unsigned 64-bit integer
   LOCAL vName AS VARIANT                            ' // String
   LOCAL vNetConnectionID AS VARIANT                 ' // String
   LOCAL vNetConnectionStatus AS VARIANT             ' // Unsigned 16-bit integer
   LOCAL vNetEnabled AS VARIANT                      ' // Boolean value
   LOCAL vNetworkAddresses AS VARIANT                ' // Array - String
   LOCAL vPermanentAddress AS VARIANT                ' // String
   LOCAL vPhysicalAdapter AS VARIANT                 ' // Boolean value
   LOCAL vPNPDeviceID AS VARIANT                     ' // String
   LOCAL vPowerManagementCapabilities AS VARIANT     ' // Array - Unsigned 16-bit integer
   LOCAL vPowerManagementSupported AS VARIANT        ' // Boolean value
   LOCAL vProductName AS VARIANT                     ' // String
   LOCAL vServiceName AS VARIANT                     ' // String
   LOCAL vSpeed AS VARIANT                           ' // Unsigned 64-bit integer
   LOCAL vStatus AS VARIANT                          ' // String
   LOCAL vStatusInfo AS VARIANT                      ' // Unsigned 16-bit integer
   LOCAL vSystemCreationClassName AS VARIANT         ' // String
   LOCAL vSystemName AS VARIANT                      ' // String
   LOCAL vTimeOfLastReset AS VARIANT                 ' // Date/time value

   ' // Connect to WMI using a moniker
   bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
   pService = WmiGetObject(bstrDisplayName)
   IF ISNOTHING(pService) THEN EXIT FUNCTION

   ' // Execute a query to get a reference to the collection of objects
   bstrQuery = "SELECT * FROM Win32_NetworkAdapter"
   pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
   IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
   ' // Retrieve a reference to the collection's enumerator
   pEnum = pObjectSet.NewEnum_
   IF ISNOTHING(pEnum) THEN EXIT FUNCTION
   ' // Iterate through the collection of objects
   DO
      ' // Retrieve a reference to the next object in the collection
      hr = pEnum.Next(1, vItem, BYVAL %NULL)
      IF hr <> %S_OK THEN EXIT DO
      ' // Assign the VT_DISPATCH variant to the object variable
      oItem = vItem : vItem = EMPTY
      IF ISNOTHING(oItem) THEN EXIT DO
      ' // Retrieve the values of the properties
      OBJECT GET oItem.AdapterType TO vAdapterType
? VARIANT$$(vAdapterType)
      OBJECT GET oItem.AdapterTypeId TO vAdapterTypeId
      OBJECT GET oItem.AutoSense TO vAutoSense
      OBJECT GET oItem.Availability TO vAvailability
      OBJECT GET oItem.Caption TO vCaption
      OBJECT GET oItem.ConfigManagerErrorCode TO vConfigManagerErrorCode
      OBJECT GET oItem.ConfigManagerUserConfig TO vConfigManagerUserConfig
      OBJECT GET oItem.CreationClassName TO vCreationClassName
      OBJECT GET oItem.Description TO vDescription
      OBJECT GET oItem.DeviceID TO vDeviceID
      OBJECT GET oItem.ErrorCleared TO vErrorCleared
      OBJECT GET oItem.ErrorDescription TO vErrorDescription
      OBJECT GET oItem.GUID TO vGUID
      OBJECT GET oItem.Index TO vIndex
      OBJECT GET oItem.InstallDate TO vInstallDate
      OBJECT GET oItem.Installed TO vInstalled
      OBJECT GET oItem.InterfaceIndex TO vInterfaceIndex
      OBJECT GET oItem.LastErrorCode TO vLastErrorCode
      OBJECT GET oItem.MACAddress TO vMACAddress
? VARIANT$$(vMACAddress)
      OBJECT GET oItem.Manufacturer TO vManufacturer
      OBJECT GET oItem.MaxNumberControlled TO vMaxNumberControlled
      OBJECT GET oItem.MaxSpeed TO vMaxSpeed
      OBJECT GET oItem.Name TO vName
      OBJECT GET oItem.NetConnectionID TO vNetConnectionID
      OBJECT GET oItem.NetConnectionStatus TO vNetConnectionStatus
      OBJECT GET oItem.NetEnabled TO vNetEnabled
      OBJECT GET oItem.NetworkAddresses TO vNetworkAddresses
      OBJECT GET oItem.PermanentAddress TO vPermanentAddress
      OBJECT GET oItem.PhysicalAdapter TO vPhysicalAdapter
      OBJECT GET oItem.PNPDeviceID TO vPNPDeviceID
      OBJECT GET oItem.PowerManagementCapabilities TO vPowerManagementCapabilities
      OBJECT GET oItem.PowerManagementSupported TO vPowerManagementSupported
      OBJECT GET oItem.ProductName TO vProductName
      OBJECT GET oItem.ServiceName TO vServiceName
      OBJECT GET oItem.Speed TO vSpeed
      OBJECT GET oItem.Status TO vStatus
      OBJECT GET oItem.StatusInfo TO vStatusInfo
      OBJECT GET oItem.SystemCreationClassName TO vSystemCreationClassName
      OBJECT GET oItem.SystemName TO vSystemName
      OBJECT GET oItem.TimeOfLastReset TO vTimeOfLastReset
      ' // Release the object
      oItem = NOTHING
   LOOP

   ' // Release the enumerator
   pEnum = NOTHING
   ' // Release the collection
   pObjectSet = NOTHING
   ' // Release the service
   pService = NOTHING

END FUNCTION
' ========================================================================================


Note. It only supplies IPv4 data. For IPv6 data we need to use the MSFT_NetAdapter class instead, but it is only available in Windows 8.

Rudolf Furstauer

Jose, you are incredible!

Thank you for this example,
I'll try it on the weekend.
Interesting that we can use to query WMI SQL statements.


Many thanks,


Rudolf Fürstauer