public static void UpdateExtendedTcpTable(bool sorted, IpHelper.TcpTableType tabletype) { TcpRows.Clear(); IntPtr tcpTable = IntPtr.Zero; int tcpTableLength = 0; if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, IpHelper.AfInet, tabletype, 0) != 0) { try { tcpTable = Marshal.AllocHGlobal(tcpTableLength); if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, IpHelper.AfInet, tabletype, 0) == 0) { IpHelper.TcpTable table = (IpHelper.TcpTable)Marshal.PtrToStructure(tcpTable, typeof(IpHelper.TcpTable)); IntPtr rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.Length)); for (int i = 0; i < table.Length; ++i) { TcpRows.Add(new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow)))); rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow))); } } } finally { if (tcpTable != IntPtr.Zero) { Marshal.FreeHGlobal(tcpTable); } } } }
internal static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, IpHelper.TcpTableType tcpTableType, int reserved);