public static long KillTCPConnectionForProcess() { long startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; MibTcprowOwnerPid[] table; var afInet = 2; var buffSize = 0; var ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); var buffTable = Marshal.AllocHGlobal(buffSize); try { uint statusCode = GetExtendedTcpTable(buffTable, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); if (statusCode != 0) { return(-1); } var tab = (MibTcptableOwnerPid)Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid)); var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries)); table = new MibTcprowOwnerPid[tab.dwNumEntries]; for (var i = 0; i < tab.dwNumEntries; i++) { var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid)); table[i] = tcpRow; rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow)); } } finally { Marshal.FreeHGlobal(buffTable); } // Kill Path Connection var PathConnection = table.FirstOrDefault(t => t.owningPid == Settings.processId); PathConnection.state = 12; var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PathConnection)); Marshal.StructureToPtr(PathConnection, ptr, false); SetTcpEntry(ptr); return(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - startTime); }
public static int KillTCPConnectionForProcess(int ProcessId) { MibTcprowOwnerPid[] table; var afInet = 2; var buffSize = 0; var ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); var buffTable = Marshal.AllocHGlobal(buffSize); try { ret = GetExtendedTcpTable(buffTable, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); if (ret != 0) { return(0); } var tab = (MibTcptableOwnerPid)Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid)); var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries)); table = new MibTcprowOwnerPid[tab.dwNumEntries]; for (var i = 0; i < tab.dwNumEntries; i++) { var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid)); table[i] = tcpRow; rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow)); } } finally { Marshal.FreeHGlobal(buffTable); } //Kill Path Connection var PathConnection = table.FirstOrDefault(t => t.owningPid == ProcessId); PathConnection.state = 12; var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PathConnection)); Marshal.StructureToPtr(PathConnection, ptr, false); var tcpEntry = SetTcpEntry(ptr); return(tcpEntry); }