Question

i am trying to get the MAC address of another computer using a server and client program in c++ using UDP connections. The server is on one computer (it contains 2 listboxes, 1 for the IP addresses of connected clients, the other for the MAC address) the client is on another computer. my current code only gets the MAC address if i run the server and client and the same computer. When i debug the program i see that when trying to get the MAC address of another computer the program doesn't go into the if statement and run the line PrintMACaddress(pAdapterInfo->Address);

 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;// Contains pointer to current adapter info
    bool found = false;
    do {
        const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; 
        if (addr_str != NULL)
        {
            if (selected_ip_adr == addr_str->IpAddress.String)
            {
                PrintMACaddress(pAdapterInfo->Address);
            }              
        }   

        pAdapterInfo = pAdapterInfo->Next; 


    }
    while(pAdapterInfo);                        

}

i found that using the ARP function may help with this or since the MAC address is data i may transmit it as a string or raw data but i have idea how to this

here is the PrintMAC function:

    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); 
}
Was it helpful?

Solution

The GetAdaptersInfo function only returns the addresses of the adapters attached to the local computer. You should take a look to SendARP.

EDIT: Try this:

void CmfcServerDlg::PrintMACFromIP(const CString &selected_ip_adr)
{
DWORD dwRetVal;
IPAddr DestIp = 0;
IPAddr SrcIp = 0;       /* default for src ip */
ULONG MacAddr[2];       /* for 6-byte hardware addresses */
ULONG PhysAddrLen = 6;  /* default to length of six bytes */

char *SrcIpString = NULL;

BYTE *bPhysAddr;

DestIp = inet_addr(CT2A(selected_ip_adr));

memset(&MacAddr, 0xff, sizeof (MacAddr));

dwRetVal = SendARP(DestIp, SrcIp, &MacAddr, &PhysAddrLen);

if (dwRetVal == NO_ERROR) {
    bPhysAddr = (BYTE *) & MacAddr;
    if (PhysAddrLen) {
        CString theMac;
        theMac.Format(_T("%.2X-%.2X-%.2X-%.2X-%.2X-%.2X"), (int) bPhysAddr[0],
            (int) bPhysAddr[1],(int) bPhysAddr[2],(int) bPhysAddr[3],(int) bPhysAddr[4],
            (int) bPhysAddr[5]);
        PrintMACaddress(theMac);
    } else
        printf
            ("Warning: SendArp completed successfully, but returned length=0\n");

} else {
    printf("Error: SendArp failed with error: %d", dwRetVal);
    switch (dwRetVal) {
    case ERROR_GEN_FAILURE:
        printf(" (ERROR_GEN_FAILURE)\n");
        break;
    case ERROR_INVALID_PARAMETER:
        printf(" (ERROR_INVALID_PARAMETER)\n");
        break;
    case ERROR_INVALID_USER_BUFFER:
        printf(" (ERROR_INVALID_USER_BUFFER)\n");
        break;
    case ERROR_BAD_NET_NAME:
        printf(" (ERROR_GEN_FAILURE)\n");
        break;
    case ERROR_BUFFER_OVERFLOW:
        printf(" (ERROR_BUFFER_OVERFLOW)\n");
        break;
    case ERROR_NOT_FOUND:
        printf(" (ERROR_NOT_FOUND)\n");
        break;
    default:
        printf("\n");
        break;
    }
  }
}

void CmfcServerDlg::PrintMACaddress(CString& strText)
{
    m_ClientIdList.AddString(strText); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top