Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than iphlpapi, prefix the name with the module name and a period.
private const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
private const int MAX_ADAPTER_NAME_LENGTH = 256;
private const int MAX_DHCPV6_DUID_LENGTH = 130;
[StructLayout(LayoutKind.Sequential)]
public struct SOCKADDR
{
/// u_short->unsigned short
public ushort sa_family;
/// char[14]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
public byte[] sa_data;
}
[StructLayout(LayoutKind.Sequential)]
public struct SOCKET_ADDRESS
{
public IntPtr lpSockAddr;
public int iSockaddrLength;
}
[StructLayout(LayoutKind.Sequential)]
public struct IP_ADAPTER_UNICAST_ADDRESS
{
public UInt64 Alignment;
public IntPtr Next;
public SOCKET_ADDRESS Address;
public IP_PREFIX_ORIGIN PrefixOrigin;
public IP_SUFFIX_ORIGIN SuffixOrigin;
public IP_DAD_STATE DadState;
public uint ValidLifetime;
public uint PreferredLifetime;
public uint LeaseLifetime;
}
[StructLayout(LayoutKind.Sequential)]
public struct IP_ADAPTER_ADDRESSES
{
public UInt64 Alignment;
public IntPtr Next;
public IntPtr AdapterName;
public IntPtr FirstUnicastAddress;
public IntPtr FirstAnycastAddress;
public IntPtr FirstMulticastAddress;
public IntPtr FirstDnsServerAddress;
[MarshalAs(UnmanagedType.LPWStr)]
public string DnsSuffix;
[System.Runtime.InteropServices.MarshalAs(UnmanagedType.LPWStr)]
public string Description;
[System.Runtime.InteropServices.MarshalAs(UnmanagedType.LPWStr)]
public string FriendlyName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_ADAPTER_ADDRESS_LENGTH)]
public byte[] PhysicalAddress;
public uint PhysicalAddressLength;
public uint Flags;
public uint Mtu;
public IF_TYPE IfType;
public IF_OPER_STATUS OperStatus;
uint Ipv6IfIndex;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public uint[] ZoneIndices;
public IntPtr FirstPrefix;
// Items added for Vista
// May need to be removed on Windows versions below Vista to work properly (?)
public UInt64 TrasmitLinkSpeed;
public UInt64 ReceiveLinkSpeed;
public IntPtr FirstWinsServerAddress;
public IntPtr FirstGatewayAddress;
public uint Ipv4Metric;
public uint Ipv6Metric;
public UInt64 Luid;
public SOCKET_ADDRESS Dhcpv4Server;
public uint CompartmentId;
public GUID NetworkGuid;
public NET_IF_CONNECTION_TYPE ConnectionType;
public TUNNEL_TYPE TunnelType;
public SOCKET_ADDRESS Dhcpv6Server;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DHCPV6_DUID_LENGTH)]
public byte[] Dhcpv6ClientDuid;
public uint Dhcpv6ClientDuidLength;
public uint Dhcpv6Iaid;
public uint FirstDnsSuffix;
}
public static string MarshalString(IntPtr text)
{
// !!!: This should only be used with IP_ADAPTER_ADDRESSES.AdapterName since it can't be marshalled automatically
byte[] cName = new byte[MAX_ADAPTER_NAME_LENGTH];
Marshal.Copy(text, cName, 0, MAX_ADAPTER_NAME_LENGTH);
string name = Encoding.ASCII.GetString(cName, 0, MAX_ADAPTER_NAME_LENGTH);
public static IList<IPAddress> GetIPAddresses(FAMILY family)
{
List<IPAddress> addresses = new List<IPAddress>();
foreach (IP_ADAPTER_ADDRESSES addr in GetIPAdapters(family)) {
if (IntPtr.Zero != addr.FirstUnicastAddress)
{
IP_ADAPTER_UNICAST_ADDRESS unicastAddr = (IP_ADAPTER_UNICAST_ADDRESS)Marshal.PtrToStructure(addr.FirstUnicastAddress, typeof(IP_ADAPTER_UNICAST_ADDRESS));
if (IntPtr.Zero != unicastAddr.Address.lpSockAddr)
{
SOCKADDR socketAddr = (SOCKADDR)Marshal.PtrToStructure(unicastAddr.Address.lpSockAddr, typeof(SOCKADDR));
byte[] saData = socketAddr.sa_data.Skip(2).Take(4).ToArray();
IPAddress ipAddr = new IPAddress(saData);
addresses.Add(ipAddr);
}
}
}
return addresses;
}
/// <summary>Logic to return MAC Address for both enabled and disabled adapters.</summary>
public static List<string> GetAllMACAddresses()
{
List<string> lstMAC = new List<string>();
//Note: Possibly need logic to also go back for FAMILY.AF_INET6.
foreach (IP_ADAPTER_ADDRESSES thisAddress in GetIPAdapters(FAMILY.AF_INET, FLAGS.GAA_FLAG_INCLUDE_ALL_INTERFACES))
{
if (thisAddress.PhysicalAddressLength == 0)
{
continue;
}
else if (thisAddress.IfType != IF_TYPE.IF_TYPE_ETHERNET_CSMACD && thisAddress.IfType != IF_TYPE.IF_TYPE_IEEE80211)
{
continue;
}
else if (thisAddress.IfType == IF_TYPE.IF_TYPE_IEEE80211 && thisAddress.OperStatus == IF_OPER_STATUS.IfOperStatusUp && thisAddress.Ipv4Metric == 0 && thisAddress.Ipv6Metric == 0)
{ //Filtering Metrics only works when the adapter is enabled.
continue;
}
else if (thisAddress.Description.Contains("Virtual"))
{
continue;
}
if (thisAddress.PhysicalAddressLength == 6)
{
lstMAC.Add(BitConverter.ToString(thisAddress.PhysicalAddress, 0, 6).Replace('-', ':'));
}
else if (thisAddress.PhysicalAddressLength == 8)
{
lstMAC.Add(BitConverter.ToString(thisAddress.PhysicalAddress).Replace('-', ':'));
}
}
return lstMAC;
}
}
Notes:
AdapterName is an ASCII string, I was unable to Marshal automatically, you can use this code to get it:
byte[] cName = new byte[MAX_ADAPTER_NAME_LENGTH];
Marshal.Copy(addr.AdapterName, cName, 0, MAX_ADAPTER_NAME_LENGTH);
string name = Encoding.ASCII.GetString(cName, 0, MAX_ADAPTER_NAME_LENGTH);
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).