Article View: borland.public.cppbuilder.nativeapi
Article #9423Problems getting MAC address...
From: Mike Collins
Date: Mon, 12 Jul 2004 13:16
Date: Mon, 12 Jul 2004 13:16
113 lines
4807 bytes
4807 bytes
Hi all, in the process of developing an application and I need to get the
MAC address(s) of the various NIC on a PC. Some time ago I wrote a
SystemTools class that had a function to retrieve the MAC address based on
the an adapter number (0=1st adapter, 1=2nd adapter etc...)
When I wrote it originally, it seemed to work fine but now that I look at it
more carefully, it's not actually returning the correct MAC address - the
code looks like this:
int TSystemTools::GetMACAddress(AnsiString *szMAC, Word wAdapterNo)
{
int iRes, iRtn = -1;
char cMAC[512];
memset(cMAC, 0, sizeof(cMAC));
NCB *pNCB;
LANA_ENUM *pEnum;
ADAPTER_STATUS *pAstat;
unsigned char s_Lana[sizeof(LANA_ENUM)];
unsigned char s_Astat[sizeof(ADAPTER_STATUS)];
pNCB = new NCB;
pEnum = new LANA_ENUM;
pAstat = new ADAPTER_STATUS;
try
{
memset(pNCB, 0, sizeof(NCB));
memset(pEnum, 0, sizeof(LANA_ENUM));
memset(pAstat, 0, sizeof(ADAPTER_STATUS));
// enum
pNCB->ncb_command = NCBENUM;
pNCB->ncb_buffer = s_Lana;
pNCB->ncb_length = sizeof(LANA_ENUM);
iRes = Netbios(pNCB);
if (iRes == NRC_GOODRET)
{
memcpy(pEnum, s_Lana, sizeof(LANA_ENUM));
if ((pEnum->length > 0)&&((wAdapterNo >=
pEnum->length)||(wAdapterNo <= pEnum->length)))
{
pNCB->ncb_command = NCBRESET;
pNCB->ncb_lana_num = pEnum->lana[wAdapterNo];
Netbios(pNCB);
pNCB->ncb_command = NCBASTAT;
pNCB->ncb_buffer = s_Astat;
pNCB->ncb_length = sizeof(ADAPTER_STATUS);
memset(pNCB->ncb_callname, ' ', sizeof(NCBNAMSZ));
pNCB->ncb_callname[0] = '*';
pNCB->ncb_lana_num = pEnum->lana[wAdapterNo];
iRes = Netbios(pNCB);
if (iRes == NRC_GOODRET)
{
memcpy(pAstat, s_Astat, sizeof(ADAPTER_STATUS));
sprintf(cMAC,
"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
pAstat->adapter_address[0],
pAstat->adapter_address[1],
pAstat->adapter_address[2],
pAstat->adapter_address[3],
pAstat->adapter_address[4],
pAstat->adapter_address[5]);
szMAC->printf(cMAC);
iRtn = 0;
}
}
}
}
__finally
{
delete pEnum;
delete pAstat;
delete pNCB;
}
return iRtn;
}
- regardless of the value I pass for wAdapterNo, I still get the same value
returned which does not corrispond with any of the MAC addresses returned
from ipconfig /all. Can't really see where the problem is but I originally
took the code from an example I found on the net. Any ideas?
I also tried to get this code example to work, as a quick fix (though it
might be better as it doesn't rely on NetBios):
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to
16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer
DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
assert(dwStatus == ERROR_SUCCESS);
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
// current adapter info
do
{
// do something with pAdapterInfo->Address;
pAdapterInfo = pAdapterInfo->Next;
}
while(pAdapterInfo);
- couldn't get this to compile, added in the headers iptypes.h, Iphlpapi.h,
assert.h but got the message "Unresolved external 'GetAdaptersInfo'
referenced from ...Unit1.obj"
Could any one give me any pointers on either of these points.
Thanks in advance,
Mike C
Technical Details
Message-ID:
<40f28d13@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!newsfeed.freenet.de!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3aa0ca650455337a,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Mike Collins" <mikec@kr8.co.uk> From: "Mike Collins" <mikec@kr8.co.uk> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Problems getting MAC address... Date: Mon, 12 Jul 2004 14:16:50 +0100 Lines: 115 Organization: kr8 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 213.131.121.26 Message-ID: <40f28d13@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1089637651 213.131.121.26 (12 Jul 2004 06:07:31 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!newsfeed.freenet.de!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail