|
For the people that come after me the include files are
windows.h
iphlpapi.h
---
www.funvill.com, www.Abluestar.com
|
|
|
|
|
Ryszard,
An excellent idea to post this to Code Project... I ran into this problem last year and have re-used the following code many times since. One problem with the NetBIOS method is that not all computers have NetBIOS installed. I have found that most machines do have SNMP installed, so that is the one that I use most often. In any case, maybe you can incorporate some of this code into an updated article. If you would like me to send the original .h and .cpp files send me your e-mail address and I will pass them along.
Header File starts here.
__________________________________________________________________________
/* MacRetriever.h
* Author: Peter Donahue
* Contact (709) 891-1129 (ask for Peter)
* peter.donahue@comdev.ca
* techinterface@yahoo.ca
*
* This class simplifies the task of retrieving the MAC address of a particular
* card under MS Windows.
*/
#pragma once
#include <winsock2.h>
#include <windows.h>
#include <snmp.h>
#include <nb30.h>
typedef SNMPAPI (WINAPI *t_SnmpExtensionInit)(DWORD dwUptimeReference, HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion);
typedef SNMPAPI (WINAPI *t_SnmpExtensionQuery)(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex);
typedef SNMPAPI (WINAPI *t_SnmpUtilOidCpy)(AsnObjectIdentifier * pOidDst, AsnObjectIdentifier * pOidSrc);
typedef int (WINAPI *t_WSAStartup)(WORD wVersionRequested, LPWSADATA lpWSAData);
typedef int (WINAPI *t_WSACleanup)();
typedef UCHAR (WINAPI *t_Netbios)(PNCB pncb);
typedef HRESULT (WINAPI *t_UuidCreate)(UUID *puuid);
class CMacRetriever{
public:
CMacRetriever(void);
~CMacRetriever(void);
bool GetMacUsingGuid(char *address, int cardIndex);
bool GetMacUsingNetBIOS(char *address, int cardIndex);
bool GetMacUsingSNMP(char *address, int cardIndex); // The call to get the MAC address.
const char* GetErrorMessage() {return m_error;}; // Returns the most recent error (should only be called after a function fails)
static void CopyAddressToString(unsigned char hexAddr[6], char *strAddr);
// These will be called automatically, but are public in case you would like to
// unload the DLLs before this object goes out of scope.
bool InitializeSNMP();
void UnInitializeSNMP();
bool InitializeNetBIOS();
void UnInitializeNetBIOS();
bool InitializeGuid();
void UnInitializeGuid();
protected:
// function pointers for SNMP and winsock
t_SnmpExtensionInit m_ptrSnmpExtensionInit;
t_SnmpExtensionQuery m_ptrSnmpExtensionQuery;
t_SnmpUtilOidCpy m_ptrSnmpUtilOidCpy;
t_WSAStartup m_ptrWSAStartup;
t_WSACleanup m_ptrWSACleanup;
// dll handles for SNMP and winsock
HINSTANCE m_snmpApiDll;
HINSTANCE m_inetDll;
HINSTANCE m_wsockDll;
bool m_snmpInitialized;
// function pointer for Netbios
bool m_netbiosInitialized;
HINSTANCE m_netbiosDll;
t_Netbios m_ptrNetbios;
//function pointer for UuidCreate
bool m_uuidInitialized;
HINSTANCE m_uuidDll;
t_UuidCreate m_ptrUuidCreate;
char m_error[256]; // This string will contain an error message if any function fails.
};
Source File starts here.
__________________________________________________________________________
/* MacRetriever.cpp
* Author: Peter Donahue
* Contact (709) 891-1129 (ask for Peter)
* peter.donahue@comdev.ca
* techinterface@yahoo.ca
*
* This class simplifies the task of retrieving the MAC address of a particular
* card under MS Windows.
*/
// Uncomment this line if you are using VC++ precompiled headers through stdafx.h.
//#include "stdafx.h"
#include "MacRetriever.h"
CMacRetriever::CMacRetriever(void){
m_ptrSnmpExtensionInit=NULL;
m_ptrSnmpExtensionQuery=NULL;
m_ptrSnmpUtilOidCpy=NULL;
m_ptrWSAStartup=NULL;
m_ptrWSACleanup=NULL;
m_snmpApiDll=NULL;
m_inetDll=NULL;
m_wsockDll=NULL;
m_snmpInitialized=false;
m_ptrNetbios=NULL;
m_netbiosDll=NULL;
m_netbiosInitialized=false;
m_ptrUuidCreate=NULL;
m_uuidDll=NULL;
m_uuidInitialized=false;
}
CMacRetriever::~CMacRetriever(void){
UnInitializeSNMP();
UnInitializeNetBIOS();
UnInitializeGuid();
}
// Address must be at least 13 characters long.
bool CMacRetriever::GetMacUsingSNMP(char *address, int cardIndex){
// This function used to be implemented using GUID, but that approach doesn't work
// for Win2000... This new approach uses SNMP. Hopefully it will be a little more
// robust.
address[0]='\0';
if (!InitializeSNMP()) return false;
// Get the <addressNum> MAC address
long errorStatus=0;
long errorIndex=0;
SnmpVarBindList bindList;
SnmpVarBind bindVar;
AsnObjectIdentifier tmpName;
memset(&bindVar, 0, sizeof(bindVar));
memset(&bindVar, 0, sizeof(bindList));
bindList.len=1;
bindList.list=&bindVar;
const UINT MAC_ADDRESS_OID[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6};
tmpName.idLength=10;
tmpName.ids=(UINT*)MAC_ADDRESS_OID;
m_ptrSnmpUtilOidCpy(&bindVar.name, &tmpName);
for(int i=0;i<cardIndex;++i){
if (!m_ptrSnmpExtensionQuery(ASN_RFC1157_GETNEXTREQUEST, &bindList, &errorStatus, &errorIndex)){
strcpy(m_error, "Error getting address from SNMP");
return false;
}
}
// bindVar should now have the address.
unsigned char *addr=bindVar.value.asnValue.address.stream;
if (!addr || addr==(UCHAR*)1){
strcpy(m_error, "No address found at this index");
return false;
}
// Copy addr into address
address[0]='\0';
CopyAddressToString(addr,address);
return true;
}
void CMacRetriever::UnInitializeSNMP(void){
if (m_ptrWSACleanup) m_ptrWSACleanup();
if (m_inetDll) FreeLibrary(m_inetDll);
if (m_snmpApiDll) FreeLibrary(m_snmpApiDll);
if (m_wsockDll) FreeLibrary(m_wsockDll);
m_ptrWSACleanup=NULL;
m_inetDll=NULL;
m_snmpApiDll=NULL;
m_wsockDll=NULL;
m_snmpInitialized=false;
}
// Load the SNMP and winsock dlls and get the required function pointers.
bool CMacRetriever::InitializeSNMP(void){
if (m_snmpInitialized) return true;
// Load DLLs
m_wsockDll=LoadLibrary("ws2_32.dll");
if (!m_wsockDll){
strcpy(m_error,"Unable to load inetmib1.dll (SNMP dll)");
return false;
}
m_inetDll = LoadLibrary("inetmib1.dll");
if (!m_inetDll){
strcpy(m_error,"Unable to load inetmib1.dll (SNMP dll)");
return false;
}
m_snmpApiDll=LoadLibrary("snmpapi.dll");
if (!m_snmpApiDll){
strcpy(m_error,"Unable to load snmpapi.dll (SNMP dll)");
return false;
}
// Get the SNMP function pointers.
m_ptrSnmpExtensionInit=(t_SnmpExtensionInit)::GetProcAddress(m_inetDll, "SnmpExtensionInit");
m_ptrSnmpExtensionQuery=(t_SnmpExtensionQuery)::GetProcAddress(m_inetDll, "SnmpExtensionQuery");
m_ptrSnmpUtilOidCpy=(t_SnmpUtilOidCpy)::GetProcAddress(m_snmpApiDll, "SnmpUtilOidCpy");
if (!m_ptrSnmpExtensionInit || !m_ptrSnmpExtensionQuery
|| !m_ptrSnmpUtilOidCpy){
strcpy(m_error, "Error getting functions from SNMP dll. You may not have the correct version of SNMP installed.");
return false;
}
// Get the winsock pointers.
m_ptrWSAStartup=(t_WSAStartup)::GetProcAddress(m_wsockDll, "WSAStartup");
m_ptrWSACleanup=(t_WSACleanup)::GetProcAddress(m_wsockDll, "WSACleanup");
if (!m_ptrWSAStartup || !m_ptrWSACleanup){
strcpy(m_error, "Error getting functions from winsock dll. Your winsock libs may not be installed properly.");
// Set to null so they won't be called from UnInitialize
m_ptrWSACleanup=NULL;
return false;
}
// Call init routine for winsock.
WSADATA sockData;
if (m_ptrWSAStartup(MAKEWORD(2, 0), &sockData)){
strcpy(m_error,"Winsock init failed.");
m_ptrWSACleanup=NULL;
return false;
}
// Call init routine for SNMP
HANDLE eventTrap;
AsnObjectIdentifier id;
if (!m_ptrSnmpExtensionInit(0, &eventTrap, &id)){
strcpy(m_error, "SNMP init failed.");
return false;
}
m_snmpInitialized=true;
return true;
}
typedef struct tagASTAT{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT;
// This function can only handle a single address for the machine.
bool CMacRetriever::GetMacUsingGuid(char *address, int cardIndex){
if (!InitializeGuid()) return false;
address[0]='\0';
if (cardIndex){
strcpy(m_error, "CardIndex must be 0 when calling GetMacUsingGuid. If you need other MACs use GetMacUsingNetBIOS or GetMacUsingSNMP.");
return false;
}
UUID uuid;
if (m_ptrUuidCreate(&uuid) != RPC_S_OK){
strcpy(m_error, "UuidCreate returned an invalid GUID");
return false;
}
CopyAddressToString(&uuid.Data4[2], address);
return true;
}
bool CMacRetriever::GetMacUsingNetBIOS(char *address, int cardIndex){
// Taken from MSDN article on finding the MAC address through NetBios.
address[0]='\0';
if (!InitializeNetBIOS()) return false;
NCB ncb;
ASTAT Adapter;
memset(&ncb,0,sizeof(ncb));
ncb.ncb_command=NCBRESET;
ncb.ncb_lana_num=cardIndex;
if (m_ptrNetbios(&ncb) != NRC_GOODRET){
switch(ncb.ncb_retcode){
case 0x23:
strcpy(m_error, "No NetBIOS interface found at this index");
break;
default:
strcpy(m_error, "Error calling NetBIOS reset function");
}
return false;
}
memset(&ncb,0,sizeof(ncb));
ncb.ncb_command=NCBASTAT;
ncb.ncb_lana_num=cardIndex;
strcpy((char*)ncb.ncb_callname,"* ");
ncb.ncb_buffer=(unsigned char*)&Adapter;
ncb.ncb_length=sizeof(Adapter);
if (m_ptrNetbios(&ncb) != NRC_GOODRET){
strcpy(m_error, "MAC address retrieval from NetBIOS failed");
return false;
}
CopyAddressToString(Adapter.adapt.adapter_address, address);
return true;
}
void CMacRetriever::CopyAddressToString(unsigned char hexAddr[6], char *strAddr){
char tmp[3];
strAddr[0]='\0';
for(int i=0;i<6;++i){
itoa(hexAddr[i], tmp, 16);
if (hexAddr[i] <= 0x0f) strcat(strAddr, "0");
strcat(strAddr, tmp);
}
}
void CMacRetriever::UnInitializeNetBIOS(void){
if (m_netbiosDll) FreeLibrary(m_netbiosDll);
m_netbiosDll=NULL;
m_netbiosInitialized=false;
}
bool CMacRetriever::InitializeNetBIOS(void){
if (m_netbiosInitialized) return true;
// Load DLLs
m_netbiosDll=LoadLibrary("Netapi32.dll");
if (!m_netbiosDll){
strcpy(m_error,"Unable to load Netapi32.dll (Netbios dll)");
return false;
}
// Get the NetBIOS function pointer.
m_ptrNetbios=(t_Netbios)::GetProcAddress(m_netbiosDll, "Netbios");
if (!m_ptrNetbios){
strcpy(m_error, "Error getting function from NetBIOS dll");
return false;
}
m_netbiosInitialized=true;
return true;
}
void CMacRetriever::UnInitializeGuid(void){
if (m_uuidDll) FreeLibrary(m_uuidDll);
m_uuidDll=NULL;
m_uuidInitialized=false;
}
bool CMacRetriever::InitializeGuid(void){
// We need a function to return a valid GUID (UUID including MAC address)
if (m_uuidInitialized) return true;
// Load DLLs
m_uuidDll=LoadLibrary("rpcrt4.dll");
if (!m_uuidDll){
strcpy(m_error,"Unable to load rpcrt.dll (RPC dll for UuidCreate)");
return false;
}
// Get the UuidCreate function pointer.
m_ptrUuidCreate=(t_UuidCreate)::GetProcAddress(m_uuidDll, "UuidCreateSequential");
if (!m_ptrUuidCreate){
// We may be on an older OS. Try to get old function
m_ptrUuidCreate=(t_UuidCreate)::GetProcAddress(m_uuidDll, "UuidCreate");
}
if (!m_ptrUuidCreate){
strcpy(m_error, "Error getting UuidCreate function from rpcrt4.dll");
return false;
}
m_uuidInitialized=true;
return true;
}
Test Program starts here.
____________________________________________________________________________
#include "MacRetriever.h"
#include <stdio.h>
int main(){
CMacRetriever mac;
int i;
char address[13];
printf("SNMP:\n");
for(i=0;i<6;++i){
if (mac.GetMacUsingSNMP(address,i)){
printf("mac address: %s\n", address);
}else{
printf("Error: %s\n", mac.GetErrorMessage());
}
}
printf("\nNetBIOS:\n");
for(i=0;i<6;++i){
if (mac.GetMacUsingNetBIOS(address,i)){
printf("mac address: %s\n", address);
}else{
printf("Error: %s\n", mac.GetErrorMessage());
}
}
printf("\nGUID:\n");
for(i=0;i<6;++i){
if (mac.GetMacUsingGuid(address,i)){
printf("mac address: %s\n", address);
}else{
printf("Error: %s\n", mac.GetErrorMessage());
}
}
#ifdef _DEBUG
Sleep(10000);
#endif
}
____________________________
Peter Donahue
Code Monkey
COM DEV International R&D
Cambridge, ON, Canada
|
|
|
|
|
I have tested it. it works fine.
Thank you very much.
|
|
|
|
|
Your code is useful. Thanks.
However, I've got a question here.
In the main function, why do we have to make loop for 6 times for each approach???
i know any number less than 6 is not enough, but why?
|
|
|
|
|
If anybody is still reading this thread, the 6 is completely arbitrary. I found that most machines will have less than 6 interfaces (not all interfaces are real network cards)
Pete
____________________________
Peter Donahue
Software Developer
COM DEV Space
Cambridge, ON, Canada
|
|
|
|
|
Dear All,
I had include the .h and .cpp,
but I got many errors,following this:
c:\test\testnet\macretriever.h(25) : error C2065: 'UUID' : undeclared identifier
c:\test\testnet\macretriever.h(25) : error C2065: 'puuid' : undeclared identifier
c:\test\testnet\macretriever.h(25) : error C2165: 'left-side modifier' : cannot modify pointers to data
c:\test\testnet\macretriever.h(25) : error C2071: 't_UuidCreate' : illegal storage class
C:\Test\TestNet\TestNet.cpp(43) : warning C4508: 'main' : function should return a value; 'void' return type assumed
MacRetriever.cpp
c:\test\testnet\macretriever.h(25) : error C2065: 'UUID' : undeclared identifier
c:\test\testnet\macretriever.h(25) : error C2065: 'puuid' : undeclared identifier
c:\test\testnet\macretriever.h(25) : error C2165: 'left-side modifier' : cannot modify pointers to data
c:\test\testnet\macretriever.h(25) : error C2071: 't_UuidCreate' : illegal storage class
C:\Test\TestNet\MacRetriever.cpp(191) : error C2146: syntax error : missing ';' before identifier 'uuid'
C:\Test\TestNet\MacRetriever.cpp(191) : error C2065: 'uuid' : undeclared identifier
C:\Test\TestNet\MacRetriever.cpp(192) : error C2065: 'RPC_S_OK' : undeclared identifier
C:\Test\TestNet\MacRetriever.cpp(197) : error C2228: left of '.Data4' must have class/struct/union type
C:\Test\TestNet\MacRetriever.cpp(244) : error C2065: 'itoa' : undeclared identifier
Generating Code...
Error executing cl.exe.
TestNet.exe - 13 error(s), 1 warning(s)
Then I include the Rpcdce.h,I got following this:
c:\program files\microsoft visual studio\vc98\include\rpcdce.h(30) : error C2146: syntax error : missing ';' before identifier 'RPC_BINDING_HANDLE'
c:\program files\microsoft visual studio\vc98\include\rpcdce.h(30) : fatal error C1004: unexpected end of file found
MacRetriever.cpp
c:\program files\microsoft visual studio\vc98\include\rpcdce.h(30) : error C2146: syntax error : missing ';' before identifier 'RPC_BINDING_HANDLE'
c:\program files\microsoft visual studio\vc98\include\rpcdce.h(30) : fatal error C1004: unexpected end of file found
Generating Code...
Error executing cl.exe.
TestNet.exe - 4 error(s), 0 warning(s)
What can i do?
thanks,
Jason
|
|
|
|
|
Jason,
I just copied the code from my original post and it compiled fine under both VS2005 and VC98. It must be something in your project settings. I'll e-mail both projects to you to try out. If you are reading this and you haven't received them please send me an e-mail:
peter.donahue@comdev.ca
Pete
____________________________
Peter Donahue
Software Developer
COM DEV Space
Cambridge, ON, Canada
|
|
|
|
|
Another thought for a quick fix would be to try changing UUID to GUID since they are the same data type.
Pete
____________________________
Peter Donahue
Software Developer
COM DEV Space
Cambridge, ON, Canada
|
|
|
|
|
Hi Jason,
Have you solved the problem, because I have a same kind of error using Visual C++ 6.0 on Windows XP
error C2146: syntax error : missing ';' before identifier 'GetMacAddress'
error C2501: 'CString' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found
Thanks,
Claude
|
|
|
|
|
This one works well if you only have one NIC:
// GetMACAddress()
// Returns:
// CString: MAC Address. If address is 00-00-00-00-00-00 then no adapter exists.
CString GetMACAddress()
{
CString strReturn;
GUID uuid;
// In Win2k or higher, CoCreateGuid no longer returns a mac address, but UuidCreate
// Sequential does.
typedef RPC_STATUS (CALLBACK* UuidCreateSequential_t)(UUID*);
// This doesn't exist Pre win2k,
// in that case it uses CoCreateGuid (which will fail in 2k+) - mc
HMODULE hRpcrt4 = LoadLibrary("rpcrt4.dll"); // Attempt to load RPCRT4.DLL
if(hRpcrt4)
{
UuidCreateSequential_t fpUuidCreateSequential;
fpUuidCreateSequential = (UuidCreateSequential_t)
GetProcAddress(hRpcrt4, "UuidCreateSequential");
// Attempt dynamic load of UuidCreateSequential c
if(fpUuidCreateSequential)
{
// Create Sequential UUID for determination of MAC Address
fpUuidCreateSequential(&uuid);
}
else
{ // OS Doesn't support UuidCreateSequential, so fall back on CoCreateGuid
CoCreateGuid(&uuid);
}
FreeLibrary(hRpcrt4);
}
else
{ // OS Doesn't support UuidCreateSequential, so fall back on CoCreateGuid
CoCreateGuid(&uuid);
}
// Now Format The adapter address - mc
strReturn.Format("%2.2X-%2.2X-%2.2X-%2.2X-%2.2X-%2.2X",
uuid.Data4[2],
uuid.Data4[3],
uuid.Data4[4],
uuid.Data4[5],
uuid.Data4[6],
uuid.Data4[7]);
return strReturn;
}
|
|
|
|
|
it is a very goog method!
thanks a lot!;)
|
|
|
|
|
Why are you not using simple and clean ARP request
Goran
|
|
|
|
|
hahaha, why dont make things looks complicate ?
|
|
|
|
|
hahaha, why dont make things looks complicate ?
|
|
|
|
|
This code is nearly the same as ID: Q118623 but not as useful?!
|
|
|
|
|
It is very much like the Q article .. why did this get posted?
|
|
|
|
|
And will it work if secondary NIC has netbios on it, but primary does not? If yes, will it return primary or secondary?
Igor Proskuriakov
|
|
|
|
|
I don't know. This is only simple function. I tested it on simple configuration (one NIC with netbios). This funcnction doesn't work on multiple NICs. For extended version see Q118623 in KB.
|
|
|
|
|
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
|
|
|
|
|
Does this require the Netbios protocol to be installed on the machine or will it work regardless?
|
|
|
|
|
I could be wrong, but I assumed that Netbios had to be installed AND bound to the NIC.
|
|
|
|
|
This should not be allowed as a submission
|
|
|
|
|
I'm sorry but finding the MAC address of a network card is a very frequently asked question and so can be useful to a lot of people...
Serge
|
|
|
|
|
You're joking, right?
There have been no fewer than 5 requests for code that does this very thing in the last 2 months.
Do you know Akbar?
|
|
|
|
|
Akbar? Isn't that the guy from Romeo Must Die?
|
|
|
|
|