private void GetPerAdapterInfo(uint index)
        {
            if (index != 0)
            {
                uint size = 0;

                uint result = Interop.IpHlpApi.GetPerAdapterInfo(index, IntPtr.Zero, ref size);
                while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
                {
                    // Now we allocate the buffer and read the network parameters.
                    IntPtr buffer = Marshal.AllocHGlobal((int)size);
                    try
                    {
                        result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, ref size);
                        if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                        {
                            Interop.IpHlpApi.IpPerAdapterInfo ipPerAdapterInfo =
                                Marshal.PtrToStructure <Interop.IpHlpApi.IpPerAdapterInfo>(buffer);

                            _autoConfigEnabled = ipPerAdapterInfo.autoconfigEnabled;
                            _autoConfigActive  = ipPerAdapterInfo.autoconfigActive;
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(buffer);
                    }
                }

                if (result != Interop.IpHlpApi.ERROR_SUCCESS)
                {
                    throw new NetworkInformationException((int)result);
                }
            }
        }
Exemple #2
0
        private void GetPerAdapterInfo(uint index)
        {
            if (index != 0)
            {
                uint size = 0;
                SafeLocalAllocHandle buffer = null;

                uint result = Interop.IpHlpApi.GetPerAdapterInfo(index, SafeLocalAllocHandle.Zero, ref size);
                while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
                {
                    // Now we allocate the buffer and read the network parameters.
                    using (buffer = SafeLocalAllocHandle.LocalAlloc((int)size))
                    {
                        result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, ref size);
                        if (result == Interop.IpHlpApi.ERROR_SUCCESS)
                        {
                            Interop.IpHlpApi.IpPerAdapterInfo ipPerAdapterInfo =
                                Marshal.PtrToStructure <Interop.IpHlpApi.IpPerAdapterInfo>(buffer.DangerousGetHandle());

                            _autoConfigEnabled = ipPerAdapterInfo.autoconfigEnabled;
                            _autoConfigActive  = ipPerAdapterInfo.autoconfigActive;
                        }
                    }
                }

                if (result != Interop.IpHlpApi.ERROR_SUCCESS)
                {
                    throw new NetworkInformationException((int)result);
                }
            }
        }