コード例 #1
0
ファイル: WindowsConnections.cs プロジェクト: PeaRoxy/PeaRoxy
        /// <summary>
        /// The get ex tcp connections v 2.
        /// </summary>
        /// <returns>
        /// The <see>
        ///         <cref>List</cref>
        ///     </see>
        ///     .
        /// </returns>
        private static List <ConnectionInfo> GetExTcpConnectionsV2()
        {
            List <ConnectionInfo> list   = new List <ConnectionInfo>();
            const int             AfInet = 2; // IP_v4
            int  buffSize = 48000;
            bool firstRun = true;

exec:
            byte[] buffer = new byte[buffSize];
            Result res    =
                (Result)
                GetExtendedTcpTable(buffer, out buffSize, true, AfInet, TcpTableClass.TcpTableOwnerPidConnections, 0);

            if (res != Result.NoError)
            {
                if (!firstRun)
                {
                    return(list);
                }

                firstRun = false;
                goto exec;
            }

            int numEntries = Convert.ToInt32(buffer[0]);
            int nOffset    = 4;

            for (int i = 0; i < numEntries; i++)
            {
                while (Convert.ToInt32(buffer[nOffset]) <= 0)
                {
                    nOffset++;
                }
                nOffset += 4;
                list.Add(
                    new WindowsConnection(
                        (States)Convert.ToInt32(buffer[nOffset - 4]),
                        CommonFunctions.Bytes2IpEndPoint(buffer, ref nOffset, false),
                        CommonFunctions.Bytes2IpEndPoint(buffer, ref nOffset, true),
                        CommonFunctions.Bytes2Int(buffer, ref nOffset)));
            }

            return(list);
        }