private static int GetPIDForConn(int iTargetPort, uint iAddressType, TcpClientIDNative.TcpTableType whichTable) { IntPtr intPtr = IntPtr.Zero; uint num = 32768u; try { intPtr = Marshal.AllocHGlobal(32768); uint extendedTcpTable = TcpClientIDNative.NativeMethods.GetExtendedTcpTable(intPtr, ref num, false, iAddressType, whichTable, 0u); while (122u == extendedTcpTable) { Marshal.FreeHGlobal(intPtr); num += 2048u; intPtr = Marshal.AllocHGlobal((int)num); extendedTcpTable = TcpClientIDNative.NativeMethods.GetExtendedTcpTable(intPtr, ref num, false, iAddressType, whichTable, 0u); } if (extendedTcpTable != 0u) { int result = 0; return(result); } int num2; int ofs; int num3; if (iAddressType == 2u) { num2 = 12; ofs = 12; num3 = 24; } else { num2 = 24; ofs = 32; num3 = 56; } int num4 = ((iTargetPort & 255) << 8) + ((iTargetPort & 65280) >> 8); int num5 = Marshal.ReadInt32(intPtr); if (num5 == 0) { int result = 0; return(result); } IntPtr intPtr2 = (IntPtr)((long)intPtr + (long)num2); for (int i = 0; i < num5; i++) { if (num4 == Marshal.ReadInt32(intPtr2)) { int result = Marshal.ReadInt32(intPtr2, ofs); return(result); } intPtr2 = (IntPtr)((long)intPtr2 + (long)num3); } } finally { Marshal.FreeHGlobal(intPtr); } return(0); }
public static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref uint dwTcpTableLength, [MarshalAs(UnmanagedType.Bool)] bool sort, uint ipVersion, TcpClientIDNative.TcpTableType tcpTableType, uint reserved);