SoFunction
Updated on 2025-04-05

C/C++ Obtain the MAC address of the LAN network card through IP

C/C++ Obtain the MAC address of the LAN network card through IP

Get the MAC address through win32 SendARP

Code

#include <>
#include <iostream>
#include<>
 
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"")
 
/**
  * Obtain the network card MAC address through LAN IP
 */
std::string GetMacAddress(std::string destIpStr)
{
    IPAddr srcIp = 0;
    IPAddr destIp = 0;
    ULONG mac[2];
    ULONG macLen = 6;
    destIp = inet_addr(destIpStr.c_str());
    memset(mac,0xff,sizeof(mac));
    SendARP(destIp,srcIp,(PVOID)mac,(PULONG)&macLen);
    printf("%p\r\n",mac);
 
    // Print mac    byte* bMac = (byte*)mac;
    std::string macResult;
    for (size_t i = 0; i < macLen; i++)
    {
        char macItem[2];
        /* code */
        sprintf(macItem,"%.2X",bMac[i]);
        (macItem);
        if(i<macLen-1)
        {
            ("-");
        }
    }
    
    return macResult;
};

Call

void main(int* argc,char* argv[])
{
    // std::cout << 1111 <<std::endl;
    std::string ip;
    ("192.168.31.191");
    std::string mac = GetMacAddress(ip);
    std::cout <<"ip\tmac"<< std::endl;
    std::cout << ip.c_str() << "\t" <<mac.c_str() << std::endl;
    getchar();
};

Output

 ip     mac
192.168.31.191  08-D1-27-8F-25-EV

Method supplement

Without further ado, just upload the code

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;&gt;
#include &lt;&gt;
#pragma warning(disable:4996)

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "")

std::string getMacAddress(const std::string&amp; ipAddress) {
    // Initialize Winsock    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &amp;wsaData) != 0) {
        std::cerr &lt;&lt; "WSAStartup failed." &lt;&lt; std::endl;
        return "";
    }

    // Convert IP address to ULONG format    ULONG destIp = inet_addr(ipAddress.c_str());
    if (destIp == INADDR_NONE) {
        std::cerr &lt;&lt; "Invalid IP address." &lt;&lt; std::endl;
        WSACleanup();
        return "";
    }

    // Buffer for receiving MAC address    ULONG macAddr[2];
    ULONG macAddrLen = 6;
    memset(macAddr, 0xff, sizeof(macAddr));

    // Call SendARP function to get the MAC address    DWORD ret = SendARP(destIp, 0, macAddr, &amp;macAddrLen);
    if (ret == NO_ERROR) {
        // Convert MAC address to string format        BYTE* macBytes = (BYTE*)macAddr;
        char macStr[18];
        sprintf_s(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X",
            macBytes[0], macBytes[1], macBytes[2],
            macBytes[3], macBytes[4], macBytes[5]);
        WSACleanup();
        return std::string(macStr);
    }
    else {
        std::cerr &lt;&lt; "Failed to get MAC address. Error code: " &lt;&lt; ret &lt;&lt; std::endl;
        WSACleanup();
        return "";
    }
}

int main() {
    std::string ipAddress;
    std::cout &lt;&lt; "Enter IP address: ";
    std::cin &gt;&gt; ipAddress;

    std::string macAddress = getMacAddress(ipAddress);
    if (!()) {
        std::cout &lt;&lt; "MAC Address: " &lt;&lt; macAddress &lt;&lt; std::endl;
    }
    else {
        std::cout &lt;&lt; "Could not retrieve MAC address." &lt;&lt; std::endl;
    }

    return 0;
}

This is the article about C/C++ obtaining the MAC address of the LAN through IP. For more related content of C++ obtaining the MAC address of the LAN, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!