Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

mfc - program crashes when i try find MAC address

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
",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);                        
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe your bug is here :

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

Change the while to if (addr_str != NULL)

then

it should look like

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

pAdapterInfo = pAdapterInfo->Next; 

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

See IP_ADAPTER_INFO structure at MSDN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...