Method 3 - Using the SNMP Extension API [code]
The third method I will talk about is using the SNMP (Simple Network Management Protocol) extension in Windows to get your system's address. The protocol in my experience is anything but simple, but the code should read pretty straight-forwardly. Basically the steps are the same as with Netbios:
Get a list of adapters
Query each adapter for type and MAC address
Save the adapters that are actual NICs
I don't personally know much about SNMP, but as I said before, the code is pretty clear. For more information, refer to the following URLs:
typedef bool(WINAPI * pSnmpExtensionInit) (
IN DWORD dwTimeZeroReference,
OUT HANDLE * hPollForTrapEvent,
OUT AsnObjectIdentifier * supportedView);
typedef bool(WINAPI * pSnmpExtensionTrap) (
OUT AsnObjectIdentifier * enterprise,
OUT AsnInteger * genericTrap,
OUT AsnInteger * specificTrap,
OUT AsnTimeticks * timeStamp,
OUT RFC1157VarBindList * variableBindings);
typedef bool(WINAPI * pSnmpExtensionQuery) (
IN BYTE requestType,
IN OUT RFC1157VarBindList * variableBindings,
OUT AsnInteger * errorStatus,
OUT AsnInteger * errorIndex);
typedef bool(WINAPI * pSnmpExtensionInitEx) (
OUT AsnObjectIdentifier * supportedView);
/* Load the SNMP dll and get the addresses of the functions
necessary */
m_hInst = LoadLibrary("inetmib1.dll");
if (m_hInst < (HINSTANCE) HINSTANCE_ERROR)
{
m_hInst = NULL;
return;
}
m_Init =
(pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");
m_InitEx =
(pSnmpExtensionInitEx) GetProcAddress(m_hInst,
"SnmpExtensionInitEx");
m_Query =
(pSnmpExtensionQuery) GetProcAddress(m_hInst,
"SnmpExtensionQuery");
m_Trap =
(pSnmpExtensionTrap) GetProcAddress(m_hInst, "SnmpExtensionTrap");
m_Init(GetTickCount(), &PollForTrapEvent, &SupportedView);
/* Initialize the variable list to be retrieved by m_Query */
varBindList.list = varBind;
varBind[0].name = MIB_NULL;
varBind[1].name = MIB_NULL;
/* Copy in the OID to find the number of entries in the
Inteface table */
varBindList.len = 1; /* Only retrieving one item */
SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryNum);
ret =
m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
&errorIndex);
printf("# of adapters in this system : %in",
varBind[0].value.asnValue.number);
varBindList.len = 2;
/* Copy in the OID of ifType, the type of interface */
SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);
/* Copy in the OID of ifPhysAddress, the address */
SNMP_oidcpy(&varBind[1].name, &MIB_ifMACEntAddr);
do
{
/* Submit the query. Responses will be loaded into varBindList.
We can expect this call to succeed a # of times corresponding
to the # of adapters reported to be in the system */
ret =
m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
&errorIndex);
if (!ret)
ret = 1;
else
/* Confirm that the proper type has been returned */
ret =
SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType,
MIB_ifEntryType.idLength); if (!ret) {
j++;
dtmp = varBind[0].value.asnValue.number;
printf("Interface #%i type : %in", j, dtmp);
/* Type 6 describes ethernet interfaces */
if (dtmp == 6)
{
/* Confirm that we have an address here */
ret =
SNMP_oidncmp(&varBind[1].name, &MIB_ifMACEntAddr,
MIB_ifMACEntAddr.idLength);
if ((!ret) && (varBind[1].value.asnValue.address.stream != NULL))
{
if((varBind[1].value.asnValue.address.stream[0] == 0x44)
&& (varBind[1].value.asnValue.address.stream[1] == 0x45)
&& (varBind[1].value.asnValue.address.stream[2] == 0x53)
&& (varBind[1].value.asnValue.address.stream[3] == 0x54)
&& (varBind[1].value.asnValue.address.stream[4] == 0x00))
{
/* Ignore all dial-up networking adapters */
printf("Interface #%i is a DUN adaptern", j);
continue;
}
if ((varBind[1].value.asnValue.address.stream[0] == 0x00)
&& (varBind[1].value.asnValue.address.stream[1] == 0x00)
&& (varBind[1].value.asnValue.address.stream[2] == 0x00)
&& (varBind[1].value.asnValue.address.stream[3] == 0x00)
&& (varBind[1].value.asnValue.address.stream[4] == 0x00)
&& (varBind[1].value.asnValue.address.stream[5] == 0x00))
{
/* Ignore NULL addresses returned by other network
interfaces */
printf("Interface #%i is a NULL addressn", j);
continue;
}
sprintf(TempEthernet, "%02x%02x%02x%02x%02x%02x",
varBind[1].value.asnValue.address.stream[0],
varBind[1].value.asnValue.address.stream[1],
varBind[1].value.asnValue.address.stream[2],
varBind[1].value.asnValue.address.stream[3],
varBind[1].value.asnValue.address.stream[4],
varBind[1].value.asnValue.address.stream[5]);
printf("MAC Address of interface #%i: %sn", j,
TempEthernet);}
}
}
} while (!ret); /* Stop only on an error. An error will occur
when we go exhaust the list of interfaces to
be examined */
getch();
FreeLibrary(m_hInst);
/* Free the bindings */
SNMP_FreeVarBind(&varBind[0]);
SNMP_FreeVarBind(&varBind[1]);
}
Method 2 - COM GUID API [code]
This method uses the COM API to create a GUID (globably unique identifier) and derive the MAC address therefrom. GUIDs are used commonly to identify COM components and other objects in the system. They are computed from the MAC address (among other things) and ostensibly contain that address within them. I say ostensibly because that is not at all certain. I provide this method mostly as an example of what not to do. You may end up with you MAC address this way, but chances are you'll just end up with some random hex values.
This is pretty simple and doesn't require much explanation. We create a GUID with CoCreateGuid and dump the last six bytes into a string. These should be the MAC address, but as I said, there's no way to be certain.
// Ask COM to create a UUID for us. If this 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.
GUID uuid;
CoCreateGuid(&uuid);
// Spit the address out
char mac_addr[18];
sprintf(mac_addr,"%02X:%02X:%02X:%02X:%02X:%02X",
uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],
uuid.Data4[5],uuid.Data4[6],uuid.Data4[7]);
cout << mac_addr << endl;
getch();
return 0;
}
Article #26040: Three Ways To Get Your System's Network MAC Address
Determining The MAC Address of Your System in Three Parts
I have searched endlessly through newsgroups and websites for a simple way to determine the MAC (network interface card) adress of my system. You would think that because of the overwhelming demand (especially in newsgroups) for an answer to this question, that there would be many examples available. This is not so. These examples are as a result of tireless searching and some actual learning on my part (imagine that!). Note: none of these examples involve parsing the output of ipconfig.exe /all <g>
Table Of Contents
Objective
Method 1 - Netbios API
Method 2 - Using the COM API
Method 3 Using SNMP
Example Projects
Objective
The purpose of this article is to give easy methods of determining your MAC address. I will explain how the code works and provide sample projects to illustrate it. I am assuming that you have a grasp of the following concepts:
Borland C++Builder
Simple network concepts
Some Win32 API
Method 1 - Using the Netbios API [code]
This method of getting your computer's MAC address uses Microsoft's Netbios API. This is a set of commands that enables lower-level network support than provided by say Winsock. The disadvantage to using Netbios to determine your address is that you must have Netbios installed (not really a problem if you are on a Windows network and use file sharing). Otherwise this method is quick and accurate.
The Netbios API includes only one function, called simply Netbios. This function takes a network control block structure as its parameter, which tells it what it needs to do. The structure is defined as follows:
Focus especially on the ncb_command member. This is the member that tells Netbios what to do. We will use 3 commands in determining the MAC address. They are defined on MSDN as follows:
Command Description
NCBENUM Windows NT/2000: Enumerates LAN adapter (LANA) numbers. When this code is specified, the ncb_buffer member points to a buffer to be filled with a LANA_ENUM structure.
NCBENUM is not a standard NetBIOS 3.0 command.
NCBRESET Resets a LAN adapter. An adapter must be reset before it can accept any other NCB command that specifies the same number in the ncb_lana_num member.
NCBASTAT Retrieves the status of either a local or remote adapter. When this code is specified, the ncb_buffer member points to a buffer to be filled with an ADAPTER_STATUS structure, followed by an array of NAME_BUFFER structures.
Here are the steps to getting the MAC address(es) of your system:
Enumerate the adaptors
Reset each adaptor to get proper information from it
Query the adaptor to get the MAC address and put it in standard colon-separated format
The code below provides a simple illustration of these concepts. For more information on Netbios functions, refer to the Microsoft help files or MSDN.
using namespace std;
#define bzero(thing,sz) memset(thing,0,sz)
bool GetAdapterInfo(int adapter_num, string &mac_addr)
{
// 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 = adapter_num;
if (Netbios(&Ncb) != NRC_GOODRET) {
mac_addr = "bad (NCBRESET): ";
mac_addr += string(Ncb.ncb_retcode);
return false;
}
// Prepare to get the adapter status block
bzero(&Ncb,sizeof(Ncb);
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = adapter_num;
strcpy((char *) Ncb.ncb_callname, "*");
struct ASTAT
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff[30];
} Adapter;
bzero(&Adapter,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]));
mac_addr = acMAC;
return true;
}
else
{
mac_addr = "bad (NCBASTAT): ";
mac_addr += string(Ncb.ncb_retcode);
return false;
}
}
int 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 mac_addr;
for (int i = 0; i < AdapterList.length ; ++i)
{
if (GetAdapterInfo(AdapterList.lana[i], mac_addr))
{
cout << "Adapter " << int (AdapterList.lana[i]) <<
"'s MAC is " << mac_addr << endl;
}
else
{
cerr << "Failed to get MAC address! Do you" << endl;
cerr << "have the NetBIOS protocol installed?" << endl;
break;
}
}