public static IPForwardTable GetRoutingTable() { IntPtr fwdTable = IntPtr.Zero; int size = 0; PInvoke.GetIpForwardTable(fwdTable, ref size, true); fwdTable = Marshal.AllocHGlobal(size); PInvoke.GetIpForwardTable(fwdTable, ref size, true); IPForwardTable forwardTable = PInvoke.ReadIPForwardTable(fwdTable); Marshal.FreeHGlobal(fwdTable); return(forwardTable); }
public static IPForwardTable ReadIPForwardTable(IntPtr tablePtr) { IPForwardTable result = (IPForwardTable)Marshal.PtrToStructure(tablePtr, typeof(IPForwardTable)); MibIPForwardRow[] table = new MibIPForwardRow[result.Size]; IntPtr p = new IntPtr(tablePtr.ToInt64() + Marshal.SizeOf(result.Size)); for (int i = 0; i < result.Size; ++i) { table[i] = (MibIPForwardRow)Marshal.PtrToStructure(p, typeof(MibIPForwardRow)); p = new IntPtr(p.ToInt64() + Marshal.SizeOf(typeof(MibIPForwardRow))); } result.Table = table; return(result); }
private static void DeleteRouteInternal(Func <MibIPForwardRow, int, bool> func) { IPForwardTable routingTable = GetRoutingTable(); List <MibIPForwardRow> filtered = routingTable.Table.Where(func).ToList(); IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MibIPForwardRow))); try { foreach (MibIPForwardRow routeEntry in filtered) { Marshal.StructureToPtr(routeEntry, ptr, false); PInvoke.DeleteIpForwardEntry(ptr); } } finally { Marshal.FreeHGlobal(ptr); } }