How to get the MAC address of the network adapter, which is used by default, i.e. through which the traffic goes to / from the Internet when using a socket?
Update: Found a code to output all network adapters via NetBios , example:
bool GetAdapterInfo(int nAdapterNum, std::string & sMAC) { // Reset the LAN adapter so that we can begin querying it NCB Ncb; memset(&Ncb, 0, sizeof(Ncb)); Ncb.ncb_command = NCBRESET; Ncb.ncb_lana_num = nAdapterNum; if (Netbios(&Ncb) != NRC_GOODRET) return false; // Prepare to get the adapter status block memset(&Ncb, 0, sizeof(Ncb)); Ncb.ncb_command = NCBASTAT; Ncb.ncb_lana_num = nAdapterNum; strcpy((char *)Ncb.ncb_callname, "*"); struct ASTAT { ADAPTER_STATUS adapt; NAME_BUFFER NameBuff[30]; } Adapter; memset(&Adapter, 0, sizeof(Adapter)); Ncb.ncb_buffer = (unsigned char *)&Adapter; Ncb.ncb_length = sizeof(Adapter); // Get the adapter's info and, if this works, return it in standard, // colon-delimited form. if (Netbios(&Ncb) == 0) { char acMAC[18]; sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X", int(Adapter.adapt.adapter_address[0]), int(Adapter.adapt.adapter_address[1]), int(Adapter.adapt.adapter_address[2]), int(Adapter.adapt.adapter_address[3]), int(Adapter.adapt.adapter_address[4]), int(Adapter.adapt.adapter_address[5])); sMAC = acMAC; return true; } else { return false; } } std::vector<std::wstring> GetMac() { std::vector<std::wstring> result; LANA_ENUM AdapterList; NCB Ncb; memset(&Ncb, 0, sizeof(NCB)); Ncb.ncb_command = NCBENUM; Ncb.ncb_buffer = (unsigned char *)&AdapterList; Ncb.ncb_length = sizeof(AdapterList); Netbios(&Ncb); std::string sMAC; for (int i = 0; i < AdapterList.length; ++i) { if (GetAdapterInfo(AdapterList.lana[i], sMAC)) result.push_back(std::wstring(sMAC.begin(), sMAC.end())); } return result; } But the question remains - how to get the default MAC address of the adapter?
c++11? - αλεχολυτC++11standard tools are available. - Alexisc++is still nothing to work with the network. At least 11, at least some other. This is eitherboost\asioorwinsock. - αλεχολυτ