我在 MFC 中有一个使用 UDP 的客户端服务器应用程序,其中服务器在列表框中显示已连接客户端的 IP 地址。如果我在同一台计算机上运行客户端和服务器,程序会显示 MAC 地址,但如果我尝试在另一台计算机上运行客户端,程序会崩溃。这是3个功能。I have an event handler for the listbox that displays the MAC address in a second listbox when an IP address is selected. PrintMACFromIP 是获取 MAC 地址的代码
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
int nIndex = m_ClientAddrList.GetCurSel();
if(nIndex < 0)
return;
CString s1;
m_ClientAddrList.GetText(nIndex, s1);
PrintMACFromIP(s1);
}
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);
}
void CmfcServerDlg:: PrintMACFromIP(const CString &selected_ip_adr)
{
IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);
DWORD dwStatus = GetAdaptersInfo(
AdapterInfo,
&dwBufLen);
assert(dwStatus == ERROR_SUCCESS);
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
bool found = false;
do {
const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList;
while(addr_str != NULL)
{
if(selected_ip_adr == addr_str->IpAddress.String)
{
found = true;
break;
}
}
if(found)
{
PrintMACaddress(pAdapterInfo->Address);
break;
}
else
{
pAdapterInfo = pAdapterInfo->Next;
}
}
while(pAdapterInfo);
}