Get computer name from MAC address

Posted on 2009-02-19
Last Modified: 2013-12-03
Is it possible to get the computer name from a MAC address?
Question by:JohnnyLai
    LVL 86

    Accepted Solution

    You can obtain the IP address from a MAC address, and from there it is only a small step to get a name from that IP address. See ("Edit (Add/Remove/Modify) ARP Tables") for the MAC-to-IP part. Once you have the IP address, you can use 'gethostbyaddr()' ( E.g. (like on the MSDN page):
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    int main(int argc, char **argv)
        // Declare and initialize variables
        WSADATA wsaData;
        int iResult;
        DWORD dwError;
        struct hostent *remoteHost;
        char *host_name;
        struct in_addr addr;
        // Validate the parameters
        if (argc != 2) {
            printf("usage: %s ipv4 address\n", argv[0]);
            printf("  to return the host\n");
            printf("       %s\n", argv[0]);
            return 1;
        // Initialize Winsock
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
        if (iResult != 0) {
            printf("WSAStartup failed: %d\n", iResult);
            return 1;
        host_name = argv[1];
    // If the user input is an alpha name for the host, use gethostbyname()
    // If not, get host by addr (assume IPv4)
        if (isalpha(host_name[0])) {        /* host address is a name */
            printf("Calling gethostbyname with %s\n", host_name);
            remoteHost = gethostbyname(host_name);
        } else {
            printf("Calling gethostbyaddr with %s\n", host_name);
            addr.s_addr = inet_addr(host_name);
            if (addr.s_addr == INADDR_NONE) {
                printf("The IPv4 address entered must be a legal address\n");
                return 1;
            } else
                remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
        if (remoteHost == NULL) {
            dwError = WSAGetLastError();
            if (dwError != 0) {
                if (dwError == WSAHOST_NOT_FOUND) {
                    printf("Host not found\n");
                    return 1;
                } else if (dwError == WSANO_DATA) {
                    printf("No data record found\n");
                    return 1;
                } else {
                    printf("Function failed with error: %ld\n", dwError);
                    return 1;
        } else {
            printf("Function returned:\n");
            printf("\tOfficial name: %s\n", remoteHost->h_name);
            printf("\tAlternate names: %s\n", remoteHost->h_aliases);
            printf("\tAddress type: ");
            switch (remoteHost->h_addrtype) {
            case AF_INET:
            case AF_INET6:
            case AF_NETBIOS:
                printf(" %d\n", remoteHost->h_addrtype);
            printf("\tAddress length: %d\n", remoteHost->h_length);
            addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
            printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
        return 0;

    Open in new window


    Author Comment

    Thansk! I will try that.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
    This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now