1

任何人都可以提供在 c#.net 中转换的任何托管 ip 帮助函数吗?我知道它应该由PInvokeing写的。

这是 IP 帮助程序功能:Microsoft

会很感激你。

这是一个例子:

public static TcpTable GetExtendedTcpTable(bool sorted)
    {
        List<TcpRow> tcpRows = new List<TcpRow>();

        IntPtr tcpTable = IntPtr.Zero;
        int tcpTableLength = 0;

        if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) != 0)
        {
            try
            {
                tcpTable = Marshal.AllocHGlobal(tcpTableLength);
                if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) == 0)
                {
                    IpHelper.TcpTable table = (IpHelper.TcpTable)Marshal.PtrToStructure(tcpTable, typeof(IpHelper.TcpTable));

                    IntPtr rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.length));
                    for (int i = 0; i < table.length; ++i)
                    {
                        tcpRows.Add(new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow))));
                        rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow)));
                    }
                }
            }
            finally
            {
                if (tcpTable != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(tcpTable);
                }
            }
        }

        return new TcpTable(tcpRows);
    }

`

这是本机功能:

[DllImport(IpHelper.DllName, SetLastError = true)]
public static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, TcpTableType tcpTableType, int reserved);
4

1 回答 1

2

您可以查看pinvoke.net

[DllImport("iphlpapi.dll", CharSet=CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);

[DllImport("iphlpapi.dll", SetLastError=true)]
static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass,int reserved);
于 2013-06-01T07:12:38.610 回答