Solved

How To Get MAC Address

Posted on 2000-03-01
3
247 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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