Click here to Skip to main content
15,845,055 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 288K   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

 
Generalthis worked for me !! Pin
app_devlpr21-Mar-10 19:49
app_devlpr21-Mar-10 19:49 
GeneralNetBios Return ErrorCode 23 (Data error (cyclic redundancy check). ) Pin
Member 376114829-Jul-09 1:59
Member 376114829-Jul-09 1:59 
GeneralNetBios API not working for Windows Vista Pin
Ankush12312-Feb-09 21:43
Ankush12312-Feb-09 21:43 
GeneralBIOS Name Pin
SalvaThor2329-Nov-07 20:20
SalvaThor2329-Nov-07 20:20 
QuestionWon't work ? Pin
Member 788166325-Sep-05 2:20
Member 788166325-Sep-05 2:20 
AnswerRe: Won't work ? Pin
joe_chen114-Jun-07 18:24
joe_chen114-Jun-07 18:24 
GeneralGet MAC Address Pin
Jagmal1-Feb-05 20:30
Jagmal1-Feb-05 20:30 
GeneraliWhat i pass to sNetBiosName Pin
equebal24-Dec-04 4:34
equebal24-Dec-04 4:34 
GeneralRe: iWhat i pass to sNetBiosName Pin
joe_chen114-Jun-07 18:25
joe_chen114-Jun-07 18:25 
QuestionWhat do I pass in? Pin
pjobson2-Aug-04 6:27
pjobson2-Aug-04 6:27 
GeneralNeed Get mac address on Foxpro 2.6 under Ms-dos Pin
morteza_mousavi9-Jul-04 2:01
sussmorteza_mousavi9-Jul-04 2:01 
GeneralNeed Get MAC Address Source Code and ActiveX Control or DLL Library for VB,VFP Pin
John Nu23-Mar-04 20:11
sussJohn Nu23-Mar-04 20:11 
Generaldetect my NIC device is connected to the LAN Pin
tangming05205-Feb-04 21:04
tangming05205-Feb-04 21:04 
GeneralIf not connected Pin
gcolsani7-Oct-03 12:00
gcolsani7-Oct-03 12:00 
Generalthis example juste come from... Pin
Member 51573621-Aug-03 6:39
Member 51573621-Aug-03 6:39 
GeneralRe: this example juste come from... Pin
Ryszard Krakowiak26-Aug-03 3:28
Ryszard Krakowiak26-Aug-03 3:28 
Questionwhy I can't pass link? Pin
shinypro8-Dec-02 22:17
shinypro8-Dec-02 22:17 
AnswerRe: why I can't pass link? Pin
Ryszard Krakowiak12-Dec-02 22:26
Ryszard Krakowiak12-Dec-02 22:26 
GeneralProtocol independent way of getting MAC Pin
Goran Car30-Aug-02 10:39
Goran Car30-Aug-02 10:39 
GeneralRe: Protocol independent way of getting MAC Pin
ETA10-Sep-03 11:37
ETA10-Sep-03 11:37 
GeneralRe: Protocol independent way of getting MAC Pin
zero.sg5-Feb-04 21:14
zero.sg5-Feb-04 21:14 
GeneralRe: Protocol independent way of getting MAC Pin
funvill7-Jul-06 12:34
funvill7-Jul-06 12:34 
GeneralMAC from multiple sources. Pin
Peter Donahue14-May-02 10:51
Peter Donahue14-May-02 10:51 
GeneralVery very good job. Pin
VCPP615-Nov-04 20:25
VCPP615-Nov-04 20:25 
GeneralRe: MAC from multiple sources. Pin
sizheng7-Jul-05 0:30
sizheng7-Jul-05 0:30 

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.