Solved

How To Get MAC Address

Posted on 2000-03-01
3
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

13 Experts available now in Live!

Get 1:1 Help Now