Exemplo n.º 1
0
        /// <summary>
        /// Remove the window from "Alt + TAB list".
        /// </summary>
        /// <param name="window">The window</param>
        /// <param name="NoActivate">No activate (get focus)</param>
        public static void WindowMissFromMission(Window window, bool NoActivate)
        {
            WindowInteropHelper helper = new WindowInteropHelper(window);
            long old = WinAPIWrapper.GetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE);

            old |= WinAPIWrapper.WS_EX_TOOLWINDOW;
            if (NoActivate)
            {
                old |= WinAPIWrapper.WS_EX_NOACTIVATE;
            }
            //Console.WriteLine("Remove the window from Alt+TAB list" + WinAPIWrapper.SetWindowLong(helper.Handle, WinAPIWrapper.GWL_EXSTYLE, (IntPtr)old));
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function reads and parses the active TCP socket connections available
        /// and stores them in a list.
        /// </summary>
        /// <returns>
        /// It returns the current set of TCP socket connections which are active.
        /// </returns>
        /// <exception cref="OutOfMemoryException">
        /// This exception may be thrown by the function Marshal.AllocHGlobal when there
        /// is insufficient memory to satisfy the request.
        /// </exception>
        public static List <TcpProcessRecord> GetAllTcpConnections()
        {
            int bufferSize = 0;
            List <TcpProcessRecord> tcpTableRecords = new List <TcpProcessRecord>(100);

            // Getting the size of TCP table, that is returned in 'bufferSize' variable.
            _ = WinAPIWrapper.GetExtendedTcpTable(IntPtr.Zero, ref bufferSize, true, AF_INET,
                                                  TcpTableClass.TCP_TABLE_OWNER_PID_ALL);

            // Allocating memory from the unmanaged memory of the process by using the
            // specified number of bytes in 'bufferSize' variable.
            IntPtr tcpTableRecordsPtr = Marshal.AllocHGlobal(bufferSize);

            try
            {
                // The size of the table returned in 'bufferSize' variable in previous
                // call must be used in this subsequent call to 'GetExtendedTcpTable'
                // function in order to successfully retrieve the table.
                uint result = WinAPIWrapper.GetExtendedTcpTable(tcpTableRecordsPtr, ref bufferSize, true,
                                                                AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL);

                // Non-zero value represent the function 'GetExtendedTcpTable' failed,
                // hence empty list is returned to the caller function.
                if (result != 0)
                {
                    return(new List <TcpProcessRecord>());
                }

                // Marshals data from an unmanaged block of memory to a newly allocated
                // managed object 'tcpRecordsTable' of type 'MIB_TCPTABLE_OWNER_PID'
                // to get number of entries of the specified TCP table structure.
                MIB_TCPTABLE_OWNER_PID tcpRecordsTable = (MIB_TCPTABLE_OWNER_PID)
                                                         Marshal.PtrToStructure(tcpTableRecordsPtr,
                                                                                typeof(MIB_TCPTABLE_OWNER_PID));
                IntPtr tableRowPtr = (IntPtr)((long)tcpTableRecordsPtr +
                                              Marshal.SizeOf(tcpRecordsTable.dwNumEntries));

                // Reading and parsing the TCP records one by one from the table and
                // storing them in a list of 'TcpProcessRecord' structure type objects.

                for (int row = 0; row < tcpRecordsTable.dwNumEntries; row++)
                {
                    MIB_TCPROW_OWNER_PID tcpRow = (MIB_TCPROW_OWNER_PID)Marshal.
                                                  PtrToStructure(tableRowPtr, typeof(MIB_TCPROW_OWNER_PID));
                    tcpTableRecords.Add(new TcpProcessRecord(tcpRow.localAddr,
                                                             tcpRow.remoteAddr,
                                                             BitConverter.ToUInt16(new byte[2] {
                        tcpRow.localPort[1],
                        tcpRow.localPort[0]
                    }, 0),
                                                             BitConverter.ToUInt16(new byte[2] {
                        tcpRow.remotePort[1],
                        tcpRow.remotePort[0]
                    }, 0),
                                                             tcpRow.owningPid, tcpRow.state));


                    tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(tcpRow));
                }
            }
            //catch (OutOfMemoryException outOfMemoryException)
            //{

            //}
            //catch (Exception exception)
            //{

            //}
            finally
            {
                Marshal.FreeHGlobal(tcpTableRecordsPtr);
            }
            return(tcpTableRecords != null?tcpTableRecords.Distinct()
                   .ToList <TcpProcessRecord>() : new List <TcpProcessRecord>());
        }