Solved

How To Get MAC Address

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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