Solved

How To Get MAC Address

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

22 Experts available now in Live!

Get 1:1 Help Now