Inheritance: IPGlobalProperties
        private static NetworkInterface[] GetNetworkInterfaces(AddressFamily family)
        {
            IpHelperErrors.CheckFamilyUnspecified(family);
            if (ComNetOS.IsPostWin2K)
            {
                return(PostWin2KGetNetworkInterfaces(family));
            }
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            if ((family != AddressFamily.Unspecified) && (family != AddressFamily.InterNetwork))
            {
                throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
            }
            SafeLocalFree pAdapterInfo = null;
            uint          pOutBufLen   = 0;
            ArrayList     list         = new ArrayList();
            uint          adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);

            while (adaptersInfo == 0x6f)
            {
                try
                {
                    pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                    if (adaptersInfo == 0)
                    {
                        IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo));
                        list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        while (ipAdapterInfo.Next != IntPtr.Zero)
                        {
                            ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(ipAdapterInfo.Next, typeof(IpAdapterInfo));
                            list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        }
                    }
                    continue;
                }
                finally
                {
                    if (pAdapterInfo != null)
                    {
                        pAdapterInfo.Close();
                    }
                }
            }
            if (adaptersInfo == 0xe8)
            {
                return(new SystemNetworkInterface[0]);
            }
            if (adaptersInfo != 0)
            {
                throw new NetworkInformationException((int)adaptersInfo);
            }
            SystemNetworkInterface[] interfaceArray = new SystemNetworkInterface[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                interfaceArray[i] = (SystemNetworkInterface)list[i];
            }
            return(interfaceArray);
        }
        private static SystemNetworkInterface[] PostWin2KGetNetworkInterfaces(AddressFamily family)
        {
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            SystemNetworkInterface[] adaptersAddresses = null;
Label_0008:
            try
            {
                adaptersAddresses = GetAdaptersAddresses(family, fixedInfo);
            }
            catch (NetworkInformationException exception)
            {
                if (exception.ErrorCode != 1L)
                {
                    throw;
                }
                goto Label_0008;
            }
            if (Socket.OSSupportsIPv4)
            {
                uint          pOutBufLen   = 0;
                uint          adaptersInfo = 0;
                SafeLocalFree pAdapterInfo = null;
                if ((family == AddressFamily.Unspecified) || (family == AddressFamily.InterNetwork))
                {
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);
                    int num3 = 0;
                    while (adaptersInfo == 0x6f)
                    {
                        try
                        {
                            pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                            adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                            if (adaptersInfo == 0)
                            {
                                IntPtr handle = pAdapterInfo.DangerousGetHandle();
                                while (handle != IntPtr.Zero)
                                {
                                    IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(handle, typeof(IpAdapterInfo));
                                    for (int i = 0; i < adaptersAddresses.Length; i++)
                                    {
                                        if ((adaptersAddresses[i] != null) && (ipAdapterInfo.index == adaptersAddresses[i].index))
                                        {
                                            if (!adaptersAddresses[i].interfaceProperties.Update(fixedInfo, ipAdapterInfo))
                                            {
                                                adaptersAddresses[i] = null;
                                                num3++;
                                            }
                                            break;
                                        }
                                    }
                                    handle = ipAdapterInfo.Next;
                                }
                            }
                            continue;
                        }
                        finally
                        {
                            if (pAdapterInfo != null)
                            {
                                pAdapterInfo.Close();
                            }
                        }
                    }
                    if (num3 != 0)
                    {
                        SystemNetworkInterface[] interfaceArray2 = new SystemNetworkInterface[adaptersAddresses.Length - num3];
                        int num5 = 0;
                        for (int j = 0; j < adaptersAddresses.Length; j++)
                        {
                            if (adaptersAddresses[j] != null)
                            {
                                interfaceArray2[num5++] = adaptersAddresses[j];
                            }
                        }
                        adaptersAddresses = interfaceArray2;
                    }
                }
                if ((adaptersInfo != 0) && (adaptersInfo != 0xe8))
                {
                    throw new NetworkInformationException((int)adaptersInfo);
                }
            }
            return(adaptersAddresses);
        }
        internal static NetworkInterface[] GetNetworkInterfaces()
        {
            Contract.Ensures(Contract.Result <NetworkInterface[]>() != null);
            AddressFamily family     = AddressFamily.Unspecified;
            uint          bufferSize = 0;
            SafeLocalFree buffer     = null;
            FixedInfo     fixedInfo  = SystemIPGlobalProperties.GetFixedInfo();
            List <SystemNetworkInterface> interfaceList = new List <SystemNetworkInterface>();

            GetAdaptersAddressesFlags flags =
                GetAdaptersAddressesFlags.IncludeGateways
                | GetAdaptersAddressesFlags.IncludeWins;

            // Figure out the right buffer size for the adapter information
            uint result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(
                family, (uint)flags, IntPtr.Zero, SafeLocalFree.Zero, ref bufferSize);

            while (result == IpHelperErrors.ErrorBufferOverflow)
            {
                try {
                    // Allocate the buffer and get the adapter info
                    buffer = SafeLocalFree.LocalAlloc((int)bufferSize);
                    result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(
                        family, (uint)flags, IntPtr.Zero, buffer, ref bufferSize);

                    // If succeeded, we're going to add each new interface
                    if (result == IpHelperErrors.Success)
                    {
                        // Linked list of interfaces
                        IntPtr ptr = buffer.DangerousGetHandle();
                        while (ptr != IntPtr.Zero)
                        {
                            // Traverse the list, marshal in the native structures, and create new NetworkInterfaces
                            IpAdapterAddresses adapterAddresses =
                                (IpAdapterAddresses)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddresses));
                            interfaceList.Add(new SystemNetworkInterface(fixedInfo, adapterAddresses));

                            ptr = adapterAddresses.next;
                        }
                    }
                }
                finally {
                    if (buffer != null)
                    {
                        buffer.Close();
                    }
                    buffer = null;
                }
            }

            // if we don't have any interfaces detected, return empty.
            if (result == IpHelperErrors.ErrorNoData || result == IpHelperErrors.ErrorInvalidParameter)
            {
                return(new SystemNetworkInterface[0]);
            }

            // Otherwise we throw on an error
            if (result != IpHelperErrors.Success)
            {
                throw new NetworkInformationException((int)result);
            }

            return(interfaceList.ToArray());
        }