Solved

How To Get MAC Address

Posted on 2000-03-01
3
249 Views
Last Modified: 2013-12-03
Could someone kindly show me a couple of lines of code to get MAC address for my Ethernet and ATM adaptors?
0
Comment
Question by:zhenteng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 195 total points
ID: 2575439
Read this:
http://support.microsoft.com/support/kb/articles/Q118/6/23.asp   
 
Or using IPHelperAPI from platform SDK
e.g:

PIP_ADAPTER_INFO pInfo;
LPBYTE pbBuffer;
ULONG ulSize = 0;
GetAdaptersInfo(NULL, &ulSize);

pbBuffer = new BYTE[ulSize];
GetAdaptersInfo((PIP_ADAPTER_INFO)pbBuffer, &ulSize);

pInfo = (PIP_ADAPTER_INFO)pbBuffer;
for(;;){
printf(_T("%s\n"), pInfo->Description);
printf(_T("MAC Address:\n"));

for(DWORD i=0; i<pInfo->AddressLength; i++){
printf(_T("%02X "), pInfo->Address[i]);
}
printf(_T("\n"));

pInfo = pInfo->Next;
if(pInfo == NULL){
break;
}
}

delete [] pbBuffer;

Regards
W.Yinan

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2575445
FYI,I post first code here:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <strstream>
#include <string>

using namespace std;

bool GetAdapterInfo(int nAdapterNum, string& sMAC)
{
    // Reset the LAN adapter so that we can begin querying it
    NCB Ncb;
    memset(&Ncb, 0, sizeof(Ncb));
    Ncb.ncb_command = NCBRESET;
    Ncb.ncb_lana_num = nAdapterNum;
    if (Netbios(&Ncb) != NRC_GOODRET) {
        char acTemp[80];
        ostrstream outs(acTemp, sizeof(acTemp));
        outs << "error " << Ncb.ncb_retcode << " on reset" << ends;
        sMAC = acTemp;
        return false;
    }

    // Prepare to get the adapter status block
    memset(&Ncb, 0, sizeof(Ncb));
    Ncb.ncb_command = NCBASTAT;
    Ncb.ncb_lana_num = nAdapterNum;
    strcpy((char*)Ncb.ncb_callname,  "*");
    struct ASTAT {
        ADAPTER_STATUS adapt;
        NAME_BUFFER NameBuff[30];
    } Adapter;
    memset(&Adapter, 0, sizeof(Adapter));
    Ncb.ncb_buffer = (unsigned char*)&Adapter;
    Ncb.ncb_length = sizeof(Adapter);

    // Get the adapter's info and, if this works, return it in standard,
    // colon-delimited form.
    if (Netbios(&Ncb) == 0) {
        char acMAC[18];
        sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
                int(Adapter.adapt.adapter_address[0]),
                int(Adapter.adapt.adapter_address[1]),
                int(Adapter.adapt.adapter_address[2]),
                int(Adapter.adapt.adapter_address[3]),
                int(Adapter.adapt.adapter_address[4]),
                int(Adapter.adapt.adapter_address[5]));
        sMAC = acMAC;
        return true;
    }
    else {
        char acTemp[80];
        ostrstream outs(acTemp, sizeof(acTemp));
        outs << "error " << Ncb.ncb_retcode << " on ASTAT" << ends;
        sMAC = acTemp;
        return false;
    }
}

void main()
{
    // Get adapter list
    LANA_ENUM AdapterList;
    NCB Ncb;
    memset(&Ncb, 0, sizeof(NCB));
    Ncb.ncb_command = NCBENUM;
    Ncb.ncb_buffer = (unsigned char*)&AdapterList;
    Ncb.ncb_length = sizeof(AdapterList);
    Netbios(&Ncb);
     
    // Get all of the local ethernet addresses
    string sMAC;
    for (int i = 0; i < AdapterList.length - 1; ++i) {
        if (GetAdapterInfo(AdapterList.lana[i], sMAC)) {
            cout << "Adapter " << int(AdapterList.lana[i]) << 
                    "'s MAC is " << sMAC << endl;
        }
        else {
            cerr << "Failed to get MAC address!  Do you" << endl;
            cerr << "have the NetBIOS protocol installed?" << endl;
            break;
        }
    }
}

Regards
W.Yinan
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2575450
Also you can use UuidCreate() to extract the MAC address.
If your machine has an Ethernet
adapter, the last six bytes of the UUID (bytes 2-7 inclusive in the Data4 element) should be the MAC address of the local  Ethernet adapter.

W.Yinan
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

740 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