[UWP] [CppWinRT] How to get mac address

ts_sinwh 41 Reputation points
2022-02-28T11:49:07.503+00:00

Hi, i'm working on a uwp app runing on hololens2.
i want get the wifi mac address.
according to this documents: windows.networking
i wrote this code.

// part of includes at pch.h  
#include <winrt/Windows.ApplicationModel.Activation.h>  
#include <winrt/Windows.ApplicationModel.Core.h>  
#include <winrt/Windows.Foundation.h>  
#include <winrt/Windows.Foundation.Collections.h>  
#include <winrt/Windows.Foundation.Metadata.h>  
#include <winrt/Windows.Data.Xml.Dom.h>  
#include <winrt/Windows.Devices.h>  
#include <winrt/Windows.Devices.WiFi.h>  
#include <winrt/Windows.Devices.WiFiDirect.h>  
#include <winrt/Windows.Devices.Sms.h>  
#include <winrt/Windows.Networking.h>  
#include <winrt/Windows.Networking.BackgroundTransfer.h>  
#include <winrt/Windows.Networking.Connectivity.h>  
#include <winrt/Windows.Networking.NetworkOperators.h>  
#include <winrt/Windows.Networking.Proximity.h>  
#include <winrt/Windows.Networking.PushNotifications.h>  
#include <winrt/Windows.Networking.ServiceDiscovery.Dnssd.h>  
#include <winrt/Windows.Networking.Sockets.h>  
#include <winrt/Windows.Networking.Vpn.h>  


// some cpp function  
  
	void MainPage::ClickLoginHandler(IInspectable const&, RoutedEventArgs const&)  
	{  
		using namespace winrt::Windows::Foundation::Collections;  
		using namespace winrt::Windows::Networking;  
		using namespace winrt::Windows::Networking::Connectivity;  
		using namespace winrt::Windows::Networking::NetworkOperators;  
		ConnectionProfile profile = NetworkInformation::GetInternetConnectionProfile();  
		NetworkOperatorTetheringManager manager = NetworkOperatorTetheringManager::CreateFromConnectionProfile(profile);  
		IVectorView<NetworkOperatorTetheringClient> clients = manager.GetTetheringClients();  
		for (NetworkOperatorTetheringClient client : clients)  
		{  
			OutputDebugString(client.MacAddress().c_str());  
			OutputDebugString(L"\r\n");  
		}  
	}  

but it reports an error on line 32 : WinRT originate error - 0x8007007F : 'The specified procedure could not be found.'。

it seems missing dll, and i can not found out which one, or two?

if you know that please let me know.

if you know other way to get mac address, please let me know.

Thanks

Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,553 questions
HoloLens Development
HoloLens Development
HoloLens: A family of Microsoft self-contained, holographic devices that enable engagement with digital content and interaction with holograms in the surrounding environment.Development: The process of researching, productizing, and refining new or existing technologies.
386 questions
{count} votes

Accepted answer
  1. Castorix31 81,941 Reputation points
    2022-03-01T12:57:18.487+00:00

    if you know other way to get mac address, please let me know.

    Another method is with GetAdaptersAddresses
    which is supported by UWP, according to the doc :
    Minimum supported client : Windows XP [desktop apps | UWP apps]

    If I do a test in UWP on Windows 10 21H1, I get :

    At beginning :

    #include <winsock2.h>  
    #include <iphlpapi.h>  
    

    Test code :

    PIP_ADAPTER_ADDRESSES pAddresses = NULL;  
    ULONG nSize;  
    ULONG nRet = GetAdaptersAddresses(AF_INET, 0, NULL, pAddresses, &nSize);  
    pAddresses = (PIP_ADAPTER_ADDRESSES)new char[nSize];  
    nRet = GetAdaptersAddresses(AF_INET, 0, NULL, pAddresses, &nSize);  
    if (nRet == ERROR_SUCCESS)  
    {      
        PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;  
        pCurrAddresses = pAddresses;  
        while (pCurrAddresses)  
        {  
            BYTE MACAddress[6] = { 0, 0, 0, 0, 0, 0 };  
            memcpy(&MACAddress, pCurrAddresses->PhysicalAddress, pCurrAddresses->PhysicalAddressLength);  
            WCHAR wsText[MAX_PATH] = TEXT("");                   
            swprintf_s(wsText, MAX_PATH,  
                L"Adapter : %s (%s) - MAC Address : %02X-%02X-%02X-%02X-%02X-%02X\r\n",  
                pCurrAddresses->Description, pCurrAddresses->FriendlyName, MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]);  
            OutputDebugString(wsText);                   
            pCurrAddresses = pCurrAddresses->Next;                    
        }  
    }  
    if (pAddresses)  
        delete pAddresses;   
    

    Result :

    Adapter : Realtek PCIe GbE Family Controller (Ethernet) - MAC Address : 40-8D-5C-77-31-2D  
    Adapter : Qualcomm Atheros AR9287 Wireless Network Adapter (Wi-Fi) - MAC Address : F4-F2-6D-88-9F-CA  
    Adapter : Microsoft Wi-Fi Direct Virtual Adapter #3 (Connexion au réseau local* 2) - MAC Address : 16-F2-6D-88-9F-CA  
    Adapter : Microsoft Wi-Fi Direct Virtual Adapter #4 (Connexion au réseau local* 4) - MAC Address : 26-F2-6D-88-9F-CA  
    Adapter : Microsoft Hosted Network Virtual Adapter #2 (Connexion au réseau local* 15) - MAC Address : 56-F2-6D-88-9F-CA  
    Adapter : TAP-Windows Adapter V9 (Ethernet TAP) - MAC Address : 00-FF-25-CE-E5-E3  
    Adapter : TunnelBear Adapter V9 (Ethernet 2) - MAC Address : 00-FF-3D-F6-FB-9B  
    Adapter : Bluetooth Device (Personal Area Network) (Connexion réseau Bluetooth) - MAC Address : 00-50-F2-E4-CF-3D  
    Adapter : Software Loopback Interface 1 (Loopback Pseudo-Interface 1) - MAC Address : 00-00-00-00-00-00  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Castorix31 81,941 Reputation points
    2022-02-28T18:54:08.567+00:00

    but it reports an error on line 32 : WinRT originate error - 0x8007007F : 'The specified procedure could not be found.'。

    Have you added, as the doc says, in the Windows app's package manifest :

    <Capabilities>
      <DeviceCapability Name="wiFiControl"/>
    </Capabilities>
    

    (but on my OS, Windows 10 21H1, I get 0 client...)