Ejemplo n.º 1
0
 private void GetPerAdapterInfo(uint index)
 {
     if (index != 0)
     {
         uint          pOutBufLen      = 0;
         SafeLocalFree pPerAdapterInfo = null;
         uint          num2            = UnsafeNetInfoNativeMethods.GetPerAdapterInfo(index, SafeLocalFree.Zero, ref pOutBufLen);
         while (num2 == 0x6f)
         {
             try
             {
                 pPerAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                 num2            = UnsafeNetInfoNativeMethods.GetPerAdapterInfo(index, pPerAdapterInfo, ref pOutBufLen);
                 if (num2 == 0)
                 {
                     IpPerAdapterInfo info = (IpPerAdapterInfo)Marshal.PtrToStructure(pPerAdapterInfo.DangerousGetHandle(), typeof(IpPerAdapterInfo));
                     this.autoConfigEnabled = info.autoconfigEnabled;
                     this.autoConfigActive  = info.autoconfigActive;
                     this.dnsAddresses      = info.dnsServerList.ToIPAddressCollection();
                 }
                 continue;
             }
             finally
             {
                 if (this.dnsAddresses == null)
                 {
                     this.dnsAddresses = new IPAddressCollection();
                 }
                 if (pPerAdapterInfo != null)
                 {
                     pPerAdapterInfo.Close();
                 }
             }
         }
         if (this.dnsAddresses == null)
         {
             this.dnsAddresses = new IPAddressCollection();
         }
         if (num2 != 0)
         {
             throw new NetworkInformationException((int)num2);
         }
     }
 }
        private void GetPerAdapterInfo(uint index)
        {
            if (index != 0)
            {
                uint          size   = 0;
                SafeLocalFree buffer = null;

                uint result = UnsafeNetInfoNativeMethods.GetPerAdapterInfo(index, SafeLocalFree.Zero, ref size);
                while (result == IpHelperErrors.ErrorBufferOverflow)
                {
                    try {
                        //now we allocate the buffer and read the network parameters.
                        buffer = SafeLocalFree.LocalAlloc((int)size);
                        result = UnsafeNetInfoNativeMethods.GetPerAdapterInfo(index, buffer, ref size);
                        if (result == IpHelperErrors.Success)
                        {
                            IpPerAdapterInfo ipPerAdapterInfo = (IpPerAdapterInfo)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(IpPerAdapterInfo));

                            autoConfigEnabled = ipPerAdapterInfo.autoconfigEnabled;
                            autoConfigActive  = ipPerAdapterInfo.autoconfigActive;
                        }
                    }
                    finally {
                        if (buffer != null)
                        {
                            buffer.Close();
                        }
                    }
                }

                if (result != IpHelperErrors.Success)
                {
                    throw new NetworkInformationException((int)result);
                }
            }
        }