程序崩溃时,我试图找到MAC地址 [英] program crashes when i try find MAC address

查看:202
本文介绍了程序崩溃时,我试图找到MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC中有一个使用UDP的客户端服务器应用程序,其中服务器在列表框中显示已连接客户端的IP地址。如果我在同一台计算机上运行客户端和服务器,程序显示MAC地址,但如果我尝试在不同的计算机上运行客户端程序崩溃。这里有3个功能。我有一个列表框的事件处理程序,当选择IP地址时,在第二个列表框中显示MAC地址。 PrintMACFromIP是获取MAC地址的代码

i have a client server application in MFC using UDP where the server displays the IP address of connected clients in a listbox. If i run the client and server on the same computer the program displays the MAC address but if i try to run the client on a different computer the program crashes. Here are the 3 functions. I have an event handler for the listbox that displays the MAC address in a second listbox when an IP address is selected. PrintMACFromIP is the code for getting the MAC address

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);                        
}


推荐答案

while(addr_str != NULL)
{
   if(selected_ip_adr == addr_str->IpAddress.String) 
   { 
      found = true;
      break;
   }
}

更改 code>到 if(addr_str!= NULL)

然后

它应该看起来像

if (add_str != NULL)
{
   if (selected_ip_adr == addr_str->IpAddress.String)
   {
      PrintMACaddress(pAdapterInfo->Address);
   }              
}   

pAdapterInfo = pAdapterInfo->Next; 

这应该处理如果pAdapterInfo是null通过使用do / while在后续个电话。

This should handle if pAdapterInfo is null by using the do/while on the subsequent next calls.

请参阅 IP_ADAPTER_INFO structure

这篇关于程序崩溃时,我试图找到MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆