mamut
October 28th, 2004, 03:37 AM
Hi,
Can anyone give me a sample code that read MAC address of the first network adapter (if any). I intend to put that code in my DriverEntry() routine.
tarantulaman
December 17th, 2004, 04:56 AM
You probably do not need this now but in case someone else does, here goes.
void GetMacAddress()
{
// Ensure buffer size is big enough to hold the address
TCHAR szBuffer[18];
DWORD dwSize = 18;
NCB ncb;
LANA_ENUM lenum;
UCHAR uMACAddress[6];
int i;
int iLEnumCnt = 1;
UCHAR uRASAdapterAddress[6] = { 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 }; // DEST
::ZeroMemory(&ncb, sizeof(NCB));
::ZeroMemory(&lenum, sizeof(lenum));
// Helper function using GetVersionEx()
if(IsOSPlatformNT())
{
// NT or better supports adapter enumeration
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (UCHAR *)&lenum;
ncb.ncb_length = sizeof(lenum);
UCHAR uRet = Netbios(&ncb);
if(uRet == NRC_GOODRET)
{
iLEnumCnt = (int)lenum.length;
}
}
else
{
// Can't do enumeration, so we'll pretend and query the first adapter only
lenum.length = (UCHAR)iLEnumCnt;
for(i=0; i<iLEnumCnt; ++i)
lenum.lana[i] = (UCHAR)i;
}
for(i = 0; i < iLEnumCnt; ++i)
{
if(ResetAdapter(lenum.lana[i]))
{
if (GetAdapterStatus(lenum.lana[i], uMACAddress))
{
// ignore RAS adapter address, we want MAC
if (memcmp(uMACAddress, uRASAdapterAddress, 6) != 0)
{
wsprintf(szBuffer, "%02X:%02X:%02X:%02X:%02X:%02X", (int)uMACAddress[0], (int)uMACAddress[1], (int)uMACAddress[2], (int)uMACAddress[3], (int)uMACAddress[4], (int)uMACAddress[5]);
}
}
}
}
}
tarantulaman
December 21st, 2004, 05:47 AM
Sorry missed out helper functions...
#include <Nb30.h>
const long lNAME_SIZE = 1024;
BOOL ResetAdapter(UCHAR lana)
{
NCB ncb;
::ZeroMemory(&ncb, sizeof(NCB));
ncb.ncb_command = NCBRESET;
ncb.ncb_lsn = 0x00;
ncb.ncb_callname[0] = 20;
ncb.ncb_callname[2] = 100;
ncb.ncb_lana_num = lana;
UCHAR uRet = Netbios(&ncb);
if(uRet == NRC_GOODRET)
return TRUE;
::SetLastError(uRet);
return FALSE;
}
BOOL GetAdapterStatus(UCHAR lana, UCHAR uMACAddress[])
{
NCB ncb;
BYTE buffer[lNAME_SIZE+2];
ADAPTER_STATUS *pAS;
::ZeroMemory(&ncb, sizeof(NCB));
::ZeroMemory(buffer, lNAME_SIZE+2);
ncb.ncb_command = NCBASTAT;
ncb.ncb_buffer = buffer;
ncb.ncb_length = lNAME_SIZE;
ncb.ncb_callname[0] = '*';
ncb.ncb_lana_num = lana;
UCHAR uRet = Netbios(&ncb);
if(uRet == NRC_GOODRET)
{
pAS = (ADAPTER_STATUS *)buffer;
memcpy(uMACAddress, &(pAS->adapter_address), 6);
return TRUE;
}
::SetLastError(uRet);
return FALSE;
}