Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / MFC
Article

Get MAC Address

Rate me:
Please Sign up or sign in to vote.
2.17/5 (6 votes)
11 May 2002 288.7K   37   51
Get MAC address function

GetMacAddress

Include nb30.h and link with netapi32.lib. This article is based on Q118623.

Source

typedef struct _ASTAT_
{
	ADAPTER_STATUS adapt;
	NAME_BUFFER    NameBuff[30];
} ASTAT, * PASTAT;


CString GetMacAddress(CString sNetBiosName)
{
    ASTAT Adapter;
	
    NCB ncb;
    UCHAR uRetCode;
	
    memset(&ncb, 0, sizeof(ncb));
    ncb.ncb_command = NCBRESET;
    ncb.ncb_lana_num = 0;
	
    uRetCode = Netbios(&ncb);
	
    memset(&ncb, 0, sizeof(ncb));
    ncb.ncb_command = NCBASTAT;
    ncb.ncb_lana_num = 0;
	
    sNetBiosName.MakeUpper();
	
    FillMemory(ncb.ncb_callname, NCBNAMSZ - 1, 0x20);
	
    strcpy((char *)ncb.ncb_callname, (LPCTSTR) sNetBiosName);
	
    ncb.ncb_callname[sNetBiosName.GetLength()] = 0x20;
    ncb.ncb_callname[NCBNAMSZ] = 0x0;
	
    ncb.ncb_buffer = (unsigned char *) &Adapter;
    ncb.ncb_length = sizeof(Adapter);
	
    uRetCode = Netbios(&ncb);
    
    CString sMacAddress;
	
    if (uRetCode == 0)
    {
    	sMacAddress.Format(_T("%02x%02x%02x%02x%02x%02x"),
    	    Adapter.adapt.adapter_address[0],
            Adapter.adapt.adapter_address[1],
            Adapter.adapt.adapter_address[2],
            Adapter.adapt.adapter_address[3],
            Adapter.adapt.adapter_address[4],
            Adapter.adapt.adapter_address[5]);
    }
    return sMacAddress;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat's wrong with using ARP request? Pin
Goran Car21-Oct-01 12:06
Goran Car21-Oct-01 12:06 
AnswerRe: What's wrong with using ARP request? Pin
7-Jul-02 6:18
suss7-Jul-02 6:18 
AnswerRe: What's wrong with using ARP request? Pin
7-Jul-02 6:18
suss7-Jul-02 6:18 
GeneralThis is a MSDN sample... Pin
12-Aug-01 23:17
suss12-Aug-01 23:17 
GeneralRe: This is a MSDN sample... Pin
Msftone17-Nov-02 14:21
Msftone17-Nov-02 14:21 
QuestionWhat about multiple NICs? Pin
Igor Proskuriakov8-Aug-01 8:13
Igor Proskuriakov8-Aug-01 8:13 
AnswerRe: What about multiple NICs? Pin
Ryszard Krakowiak8-Aug-01 19:48
Ryszard Krakowiak8-Aug-01 19:48 
AnswerRe: What about multiple NICs? Pin
David Crow16-Jun-05 4:02
David Crow16-Jun-05 4:02 
Read the Q118623 article. It shows how to loop through each NIC.


"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown


QuestionDoes this require NetBios to be installed? Pin
njc7-Aug-01 3:51
njc7-Aug-01 3:51 
AnswerRe: Does this require NetBios to be installed? Pin
9-Aug-01 10:06
suss9-Aug-01 10:06 
GeneralThis should not be allowed as a submission Pin
John Smith7-Aug-01 2:50
John Smith7-Aug-01 2:50 
GeneralRe: This should not be allowed as a submission Pin
Serge Weinstock7-Aug-01 3:40
Serge Weinstock7-Aug-01 3:40 
GeneralRe: This should not be allowed as a submission Pin
#realJSOP7-Aug-01 6:21
mve#realJSOP7-Aug-01 6:21 
GeneralRe: This should not be allowed as a submission Pin
Marc Richarme17-Aug-01 1:48
Marc Richarme17-Aug-01 1:48 
GeneralRe: This should not be allowed as a submission Pin
MyMojo1-Apr-03 1:58
MyMojo1-Apr-03 1:58 
GeneralRe: This should not be allowed as a submission Pin
7-Aug-01 9:09
suss7-Aug-01 9:09 
GeneralRe: This should not be allowed as a submission Pin
8-Aug-01 12:21
suss8-Aug-01 12:21 
GeneralRe: This should not be allowed as a submission Pin
MyMojo1-Apr-03 2:04
MyMojo1-Apr-03 2:04 
GeneralRe: This should not be allowed as a submission Pin
David Crow16-Jun-05 4:05
David Crow16-Jun-05 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.