1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
GetMACFromIP(Address, "192.168.1.1");
GetMACFromIP(Address, m_ClientIdList.GetSelectedString());
}
void CmfcServerDlg::PrintMACaddress(unsigned char MACData[])
{
CString strText;
strText.Format("%02X-%02X-%02X-%02X-%02X-%02X\n",MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
m_ClientIdList.AddString(strText);
}
//bool CmfcServerDlg::GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr)
void CmfcServerDlg:: PrintMACFromIP(const CString &selected_ip_adr)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
bool found = false;
do {
m_ClientIdList.AddString(pAdapterInfo->AdapterName);
const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; // use the address list instead of CurrentIpAddress (which is often NULL)
while(addr_str != NULL)
{
if(selected_ip_adr == addr_str->IpAddress.String) // This compares the selected ip address with an associated Adapter ip address
{
m_ClientIdList.AddString(addr_str->IpAddress.String);
found = true;
break;
}
}
if(found)
{
// memcpy(Address, pAdapterInfo->Address, MAX_ADAPTER_ADDRESS_LENGTH); // copy the adapters MAC address
PrintMACaddress(pAdapterInfo->Address);
break;
}
else
{
PrintMACaddress(pAdapterInfo->Address);
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
}
}
while(pAdapterInfo); // Terminate if last adapter
// return found; // This determines whether the provided ip address is associated with a MAC address
}
|