a Problem about MAC address

Hello there!
May someone tell me how can I get the MAC address of a computer? Is there any function
in VC++ 6.0 can do that job?
btw:Can anyone tell me ,is there any feature in the MAC Address?That is,if I input
any string that is 12 characters long ,0x0123456789AB for example, as a MAC Address,
is there any method to find out that is not a valid MAC address?
my environment:VC++6.0 Windows 2000
Best regards to you all!
[470 byte] By [ILikeOrange] at [2007-11-19 2:50:26]
# 1 Re: a Problem about MAC address
You can use the Iphlpapi (GetAdaptersInfo/GetAdaptersAddresses):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/ip_helper_functions.asp

As far as validation there is no API call that I know of that checks against known vendor blocks. I am sure though if you google you can find a DB listing all the Vendor addresses and the blocks assigned to them. I am also sure there are web sites that will allow you to plug in an address to determine the vendor of the MAC.
Mick at 2007-11-9 13:48:15 >
# 2 Re: a Problem about MAC address
well ,thank you Mick!
Your suggestion are really useful!I have learned from them very much!
Thanks again! For your kindness and help! :)
ILikeOrange at 2007-11-9 13:49:16 >
# 3 Re: a Problem about MAC address
use SendArp( .. ) from iphlpapi. BUT you will see that there are no such headers (or functions ) in VC++ 6.0!
o, you might need this attachment (iphlpapi):
chi_luci at 2007-11-9 13:50:15 >
# 4 Re: a Problem about MAC address
:D Really good things!
thanks a lot!chi_luci!
I think I really have some thing to do now, to read the codes and find out how they works! :confused:
Thanks again chi_luci!For your help and goodness! :)
ILikeOrange at 2007-11-9 13:51:20 >
# 5 Re: a Problem about MAC address
Hi

Here is some sample code, hope this helps with your understanding.

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 at 2007-11-9 13:52:19 >
# 6 Re: a Problem about MAC address
Sorry, missed other helper functions :blush:

#include <Nb30.h>

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;
}
tarantulaman at 2007-11-9 13:53:19 >
# 7 Re: a Problem about MAC address
tarantulaman, please accept my late thanks! :blush:
Until NOW,I found a new poster in my thread! :o
I run it at once,but some error happens:

error C2065: 'lNAME_SIZE' : undeclared identifier
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'buffer' : unknown size
error C2065: 'IsOSPlatformNT' : undeclared identifier

so, lNAME_SIZE = ?
and, need I add some file to my project,so that it can find the "IsOSPlatformNT" function? :confused:
Thanks again! :)
ILikeOrange at 2007-11-9 13:54:24 >
# 8 Re: a Problem about MAC address
[ Moved thread ]
Andreas Masur at 2007-11-9 13:55:18 >
# 9 Re: a Problem about MAC address
Hello ILikeOrange,

Sorry about that, I should really have tested what I posted. :blush:

Anyway here goes, the missing bits are:

// ALSO Link to - netapi32.lib

const long lNAME_SIZE = 1024;

bool IsOSPlatformNT(void)
{
bool bWindowsMinNT = false;

DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwPlatformID;

GetOSVersion(&dwPlatformID, &dwMajorVersion, &dwMinorVersion);

switch (dwPlatformID)
{
// Win 98/ME
case VER_PLATFORM_WIN32_WINDOWS:
{
bWindowsMinNT = false;
}
break;

// NT or better
case VER_PLATFORM_WIN32_NT:
{
bWindowsMinNT = true;
}
break;
}

return bWindowsMinNT;
}

bool GetOSVersion(LPDWORD dwPlatformID, LPDWORD dwMajorVersion, LPDWORD dwMinorVersion)
{
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
// If that fails, try using the OSVERSIONINFO structure.
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

if(!GetVersionEx((OSVERSIONINFO*) &osvi))
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);

if(!GetVersionEx((OSVERSIONINFO *)&osvi))
return false;
}

// Set the values
*dwMajorVersion = osvi.dwMajorVersion;
*dwMinorVersion = osvi.dwMinorVersion;
*dwPlatformID = osvi.dwPlatformId;

return true;
}


Hope this works now. ( I have now tested it and it seems to work, let me know if there are any more problems)

Dave
tarantulaman at 2007-11-9 13:56:26 >
# 10 Re: a Problem about MAC address
Thank you tarantulaman!It really works!
Because of the busy class,it had been a long time since I came here last time.When I opened the thread,I found that you had replied to me.So quickly! :)
I put the code which you provide in my project and compiled it,some small mistakes took place while Linking.At last I found that it is because I had set "Not Using MFC"! :blush: When I changed that,it worked!
Another question:Why I get five MACs ,the first three are the same and the last two are the same?Is it right?

Thank you again!tarantulaman
Best regards to you!
ILikeOrange at 2007-11-9 13:57:28 >