

The functions listed above to find out if the adapter with the given index was the primary and to find the index of the primary adapter are based on this assumption.įor instance, on a system with two network adapters, NIC1 and NIC2, let's say the adapter index for NIC1 is 65540 whereas the index for NIC2 is 65539, thus, based on the assumption I just listed, NIC2 is the primary adapter. I consistently found that the first adapter listed in response to ipconfig /all had the lowest index in the array m_pAdapters. So it appeared that my assumption above was correct. The closest thing I found as an answer was an article by CISCO. It made sense that the first adapter listed in the response to the ipconfig /all command would be the primary, but I had no way to confirm this. I could not find an authoritative answer to this question. Finding which adapter is the primary on a computer system required some research on the topic. Getting the MAC address of a network adapter using the CNetworkAdapter class was simple.

Once we have the index of the primary adapter, we can find all information about it from the array m_pAdapters.
Qt get mac address code#
I had to modify the BOOL CNetworkAdapter::SetupAdapterInfo(IP_ADAPTER_INFO* pAdaptInfo ) function by first setting the adapter address length, and then copying each byte from pAdaptInfo->Address (the MAC address) to the adapter address structure as shown below: BOOL CNetworkAdapter::SetupAdapterInfo(IP_ADAPTER_INFO* pAdaptInfo )Ĭopy Code // Rafique Sheikh: This function returns the actual // zero-based index of the Primary Adapter within the // array of Adpaters pointed to by m_pAdapters int CNetCfgDlg::GetPrimaryAdapterIndex( void)ĭWORD dwMinIndex = m_pAdapters->GetAdapterIndex() įor (unsigned int i = 0 i GetAdapterIndex() GetAdapterIndex() The groundwork for getting the MAC address of a network adapter was already laid out in Joseph's article. I also had to modify the CNetCfgDlg class to add functions to find if an adapter with a given index was the primary adapter, and to find out the index of the primary adapter and show its information. Therefore, I decided to modify the CNetworkAdapter class by adding this new functionality. I needed to get the MAC address of a network adapter as well as find out if it was the primary adapter on a multi-homed system (a system with more than one Network Interface Card). The "NetCfg - The Sequel" project in Joseph's original article provided all of the above functionality. The CNetworkAdapter class does both, it not only provides you with information but also lets you perform useful network configuration tasks such as renewing or releasing a lease on the network adapter. There are many ways to do this, one of which may be to send the "ipconfig /all" command from your application and then parse the results for specific information you are looking for however, this method gives you just the information and no real control over the network adapter.
Qt get mac address windows#
While "ipconfig /all" can be used to obtain detailed information about the IP configuration of a system, from the Windows command prompt, sometimes, we need this information in an application. It is based on an article by Joseph Dempsey: The "New ipconfig" and the IP Helper API.
Qt get mac address how to#
Finally, it shows how to get the (array) index of the primary adapter.

This article describes how to get the MAC or physical address of a network adapter, and how to tell if an adapter is the primary adapter on the system given its adapter (system) index. Note: The actual IP and MAC addresses have been replaced by fictitious values for safety reasons. QStorageInfo storage( qApp - >applicationDirPath())
