Tech Support > Microsoft Windows > Development Resources > How do I get a MAC address?
How do I get a MAC address?
Posted by Nils on October 9th, 2003


Please could someone tell me the best way to get the MAC Address of a
local machine? i.e. the machine of a user browsing a web page, where
the webpage is run by my servers.

I have had a look around and it seems to be that ActiveX and some COM
calls is the only route to go but I am a rookie in this area, so any
pointers/help would be greatly apprieciated.

Many thanks,

-Nils


+ The best things in life are free but then so are the worst.

Posted by Allan Bruce on October 9th, 2003



"Nils" <Nils.King@Software.plc.uk> wrote in message
news:cdb31147.0310090038.35a51bdc@posting.google.c om...
I have no idea how to do it via win32, but a hacked way to do it, is to do
system("ipconfig /all > tempfile");
Then open up the tempfile for the MAC address of the interfaces connected.
Allan



Posted by Jeff Henkels on October 9th, 2003


Here's how I do it; it should work on any platform from Win98 on:

#include <iphlpapi.h> // remember to link w/ iphlpapi.lib
#define IP_LOCALHOST 0x0100007F // 127.0.0.1 in network byte order

DWORD i, dwSize;
PMIB_IPADDRTABLE pAddr = NULL;
MIB_IFROW iInfo;
PFIXED_INFO pFI = NULL;
TCHAR szMAC[64];

// Get all IP addresses held by this machine; if it's connected to a
network,
// there's at least one that's not localhost
*szMAC = 0;
dwSize = 0;
GetIpAddrTable(NULL, &dwSize, TRUE);
pAddr = (PMIB_IPADDRTABLE)new BYTE[dwSize];
if (!GetIpAddrTable(pAddr, &dwSize, TRUE))
{
for (i = 0; i < pAddr->dwNumEntries; ++i)
{
if (IP_LOCALHOST != pAddr->table[i].dwAddr)
{
// Not localhost, so get the interface
memset(&iInfo, 0, sizeof(MIB_IFROW));
iInfo.dwIndex = pAddr->table[i].dwIndex;
GetIfEntry(&iInfo);

if (6 == iInfo.dwPhysAddrLen)
{
//iInfo.bPhysAddr contains the MAC address of this interface
_stprintf(szMAC, _T("%02X-%02X-%02X-%02X-%02X-%02X"),
iInfo.bPhysAddr[0], iInfo.bPhysAddr[1],
iInfo.bPhysAddr[2],
iInfo.bPhysAddr[3], iInfo.bPhysAddr[4],
iInfo.bPhysAddr[5]);
break;
}
}
}
}
delete[] (LPBYTE)pAddr;

// szMAC contains a textual representation of the MAC address of the first
active NIC



"Nils" <Nils.King@Software.plc.uk> wrote in message
news:cdb31147.0310090038.35a51bdc@posting.google.c om...


Posted by CheckAbdoul on October 9th, 2003


Take a look at the following KB article

Q118623 - HOWTO: Get the MAC Address for an Ethernet Adapter

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------

"Nils" <Nils.King@Software.plc.uk> wrote in message
news:cdb31147.0310090038.35a51bdc@posting.google.c om...


Posted by Stéphane GRAZIANO on October 9th, 2003


Read this:

http://groups.google.com/groups?selm...news.dfncis.de

I hope it will help you )

Stef++

"Allan Bruce" <allanmb@TAKEAWAYf2s.com> a écrit dans le message de
news:bm38dp$382$1@news.freedom2surf.net...


Posted by Jerry Coffin on October 10th, 2003


In article <bm38dp$382$1@news.freedom2surf.net>, allanmb@TAKEAWAYf2s.com
says...
I seem to have missed the original post in this thread, but you can
retrieve IP address(es) for the NIC(s) in a machine with
GetAdaptersInfo.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Posted by Nils on October 13th, 2003


Thanks all for you help, it gives me something to work on now :-)

Thanks,

-Nils