If the method succeeds, the return value is the number of
characters copied to the string buffer, not including the terminating
null character. A return value of zero indicates that the requested
address type has not been assigned to the adapter. If the method
fails, the return value is INET_ERROR and this typically indicates
that either the adapter index is invalid or the string buffer is not
large enough to store the complete address. To get extended error
information, call GetLastError.
The GetAdapterAddress method will return the
IPv4, IPv6 or MAC address assigned to a specific network adapter. The
primary network adapter has an index value of zero, and it increments
for each adapter that is configured on the local system.
The media access control (MAC) address is a 48 bit or 64 bit value
that is assigned to each network interface and is used for
identification and access control. All network devices on the same
subnet must be assigned their own unique MAC address. Unlike IP
addresses which may be assigned dynamically and can be frequently
changed, MAC addresses are considered to be more permanent because
they are usually assigned by the device manufacturer and stored in
firmware. Note that in some cases it is possible to change the address
assigned to a device, and virtual network interfaces may have
configurable MAC addresses.
This method returns the MAC address string as sequence of hexadecimal values
separated by a colon. An example of a 48 bit MAC address would be
"01:23:45:67:89:AB". Note that some virtual network adapters may not
have a MAC address assigned to them, in which case this method would
return zero.
This method will ignore network adapters that have been disabled, as well as
those that are bound to a virtual loopback interface. If the system
has dial-up networking or virtualization software installed, this
method may also return IP addresses assigned to a virtualized
network adapters installed by that software.
// Display the IPv4 address assigned to each network adapter
for (INT nIndex = 0;; nIndex++)
{
CString strAddress;
if (pSocket->GetAdapterAddress(nIndex, INET_ADAPTER_IPV4, strAddress) == INET_ERROR)
break;
_tprintf(_T("Adapter %d: %s\n"), nIndex, szAddress);
}