/// <summary> /// Return all network devices available on this machine through the IPHelper API /// </summary> public static NetworkDeviceList GetAllDevices() { int size = Marshal.SizeOf(typeof(IP_ADAPTER_INFO)); IntPtr buffer = Marshal.AllocHGlobal(size); uint result = GetAdaptersInfo(buffer, ref size); NetworkDeviceList deviceList = new NetworkDeviceList(); if (result == ERROR_BUFFER_OVERFLOW) { Marshal.FreeHGlobal(buffer); buffer = Marshal.AllocHGlobal(size); result = GetAdaptersInfo(buffer, ref size); } if (result == ERROR_OK) { int next = (int)buffer; IP_ADAPTER_INFO info; while (next != 0) { info = (IP_ADAPTER_INFO)Marshal.PtrToStructure((IntPtr)next, typeof(IP_ADAPTER_INFO)); next = info.Next; deviceList.Add(new NetworkDevice(info)); } return(deviceList); } else { Marshal.FreeHGlobal(buffer); throw new InvalidOperationException("GetAdaptersInfo failed: " + result); } }
/// <summary> /// Return all network devices available on this machine through the IPHelper API /// </summary> public static NetworkDeviceList GetAllDevices() { int size = Marshal.SizeOf(typeof(IP_ADAPTER_INFO)); IntPtr buffer = Marshal.AllocHGlobal(size); uint result = GetAdaptersInfo(buffer,ref size); NetworkDeviceList deviceList = new NetworkDeviceList(); if (result == ERROR_BUFFER_OVERFLOW) { Marshal.FreeHGlobal(buffer); buffer = Marshal.AllocHGlobal(size); result = GetAdaptersInfo(buffer,ref size); } if (result == ERROR_OK) { int next=(int)buffer; IP_ADAPTER_INFO info; while (next != 0) { info = (IP_ADAPTER_INFO)Marshal.PtrToStructure((IntPtr)next,typeof(IP_ADAPTER_INFO)); next=info.Next; deviceList.Add( new NetworkDevice(info) ); } return deviceList; } else { Marshal.FreeHGlobal(buffer); throw new InvalidOperationException("GetAdaptersInfo failed: " + result); } }