INT GetAdapterAddress( |
|
INT nAdapterIndex, |
|
|
INT nAddressType, |
|
|
LPTSTR lpszAddress, |
|
|
INT nMaxLength |
|
); |
INT GetAdapterAddress( |
|
INT nAdapterIndex, |
|
|
INT nAddressType, |
|
|
CString& strAddress |
|
); |
Return the IP or MAC assigned to the specified network adapter.
Parameters
- nAdapterIndex
- An integer value that identifies the network adapter.
- nAddressType
- An integer value which specifies the type of address that should
be returned:
Value |
Description |
INET_ADAPTER_IPV4 |
The address string will contain the primary IPv4 unicast
address assigned to the network adapter. |
INET_ADAPTER_IPV6 |
The address string will contain the primary IPv6 unicast
address assigned to the network adapter. |
INET_ADAPTER_MAC |
The address string will contain the media access control
(MAC) address assigned to the network adapter. |
- lpszAddress
- A string buffer that will contain the IP or MAC address assigned
to the adapter. This parameter cannot be NULL and it is recommended
that it be at least 64 characters in length to provide enough space
for any address type. An alternate form of the method accepts a CString argument
which will contain the address.
- nMaxLength
- The maximum number of characters that can be copied into the
string buffer, including the terminating null character. If the
buffer is too small to store the complete address, this method
will fail.
Return Value
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.
Remarks
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.
Example
// Display the IPv4 address assigned to each network adapter
for (INT nIndex = 0;; nIndex++)
{
CString strAddress;
if (pServer->GetAdapterAddress(nIndex, INET_ADAPTER_IPV4, strAddress) == INET_ERROR)
break;
_tprintf(_T("Adapter %d: %s\n"), nIndex, szAddress);
}
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cswsock11.h
Import Library: cswskv11.lib
See Also
EnumNetworkAddresses,
GetLocalAddress,
GetLocalName
|