示例#1
0
        public static MibTcprowOwnerPid[] GetTable()
        {
            MibTcprowOwnerPid[] tTable;
            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(null);
                }
                var tab    = (MibTcptableOwnerPid)Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid));
                var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
                tTable = new MibTcprowOwnerPid[tab.dwNumEntries];
                for (var i = 0; i < tab.dwNumEntries; i++)
                {
                    var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid));
                    tTable[i] = tcpRow;
                    rowPtr    = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffTable);
            }
            return(tTable);
        }
 public static MibTcprowOwnerPid[] GetTable()
 {
     MibTcprowOwnerPid[] tTable;
     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 null;
         var tab = (MibTcptableOwnerPid) Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid));
         var rowPtr = (IntPtr) ((long) buffTable + Marshal.SizeOf(tab.dwNumEntries));
         tTable = new MibTcprowOwnerPid[tab.dwNumEntries];
         for (var i = 0; i < tab.dwNumEntries; i++)
         {
             var tcpRow = (MibTcprowOwnerPid) Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid));
             tTable[i] = tcpRow;
             rowPtr = (IntPtr) ((long) rowPtr + Marshal.SizeOf(tcpRow));
         }
     }
     finally
     {
         Marshal.FreeHGlobal(buffTable);
     }
     return tTable;
 }
示例#3
0
文件: Utilities.cs 项目: zadop/LogOut
        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);
        }
示例#4
0
        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);
        }