1

我有以下问题。我在一个 Windows CE .NET 托管项目中为 GetIpForwardTable 函数创建了一个 PInvoke。当我调用该函数时返回结果,但结果与路由命令返回的结果不同。表中有更多条目,Mask 和 Destination 更改了位置,NextHop 始终设置为 0.0.0.0

这是类(需要调用 IPForwardEntry.GetIpForwardTable())。

public class IPForwardEntry
{
    public enum ForwardType
    {
        Other = 1,
        Invalid = 2,
        Direct = 3,
        Indirect = 4
    }

    public enum ForwardProtocol
    {
        Other = 1,
        Local = 2,
        NetMGMT = 3,
        ICMP = 4,
        EGP = 5,
        GGP = 6,
        Hello = 7,
        RIP = 8,
        IS_IS = 9,
        ES_IS = 10,
        CISCO = 11,
        BBN = 12,
        OSPF = 13,
        BGP = 14,
        NT_AUTOSTATIC = 10002,
        NT_STATIC = 10006,
        NT_STATIC_NON_DOD = 10007
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MIB_IPFORWARDROW
    {
        public uint dwForwardDest;
        public uint dwForwardMask;
        public int dwForwardPolicy;
        public uint dwForwardNextHop;
        public int dwForwardIfIndex;
        public ForwardType dwForwardType;
        public ForwardProtocol dwForwardProto;
        public int dwForwardAge;
        public int dwForwardNextHopAS;
        public int dwForwardMetric1;
        public int dwForwardMetric2;
        public int dwForwardMetric3;
        public int dwForwardMetric4;
        public int dwForwardMetric5;
    }

    private IPForwardEntry(MIB_IPFORWARDROW forwardRow)
    {
        myForwardRow = forwardRow;
    }

    private MIB_IPFORWARDROW myForwardRow;

    private const int NO_ERROR = 0;

    [DllImport("Iphlpapi.dll")]
    private static extern int CreateIpForwardEntry(MIB_IPFORWARDROW[] pRoute);

    [DllImport("Iphlpapi.dll")]
    private static extern int GetIpForwardTable(MIB_IPFORWARDROW[] pIpForwardTable, ref long pdwSize, bool bOrder);

    public static IPForwardEntry[] GetIpForwardTable()
    {
        long tableSize = 0;
        GetIpForwardTable(null, ref tableSize, true);

        MIB_IPFORWARDROW[] forwardTable = new MIB_IPFORWARDROW[tableSize / Marshal.SizeOf(typeof(MIB_IPFORWARDROW)) + 1];

        long tableSizeOld = tableSize;

        if (GetIpForwardTable(forwardTable, ref tableSize, false) != NO_ERROR)
            throw new SystemException();

        if (tableSizeOld != tableSize)
            throw new SystemException();


        IPForwardEntry[] result = new IPForwardEntry[forwardTable.Length];

        for (int i = 0; i < forwardTable.Length; i++)
            result[i] = new IPForwardEntry(forwardTable[i]);

        return result;

    }

    #region members

    public IPAddress FordwardDestination
    {
        get
        {
            return new IPAddress(myForwardRow.dwForwardDest);
        }
        set
        {
            myForwardRow.dwForwardDest = (uint) value.Address;
        }
    }

    public IPAddress ForwardMask
    {
        get
        {
            return new IPAddress(myForwardRow.dwForwardMask);
        }
        set
        {
            myForwardRow.dwForwardMask = (uint) value.Address;
        }
    }

    public int ForwardPolicy
    {
        get
        {
            return myForwardRow.dwForwardPolicy;
        }
        set
        {
            myForwardRow.dwForwardPolicy = value;
        }
    }

    public IPAddress ForwardNextHop
    {
        get
        {
            return new IPAddress(myForwardRow.dwForwardNextHop);
        }
        set
        {
            myForwardRow.dwForwardNextHop = (uint) value.Address;
        }
    }

    public int ForwardInterfaceIndex
    {
        get
        {
            return myForwardRow.dwForwardIfIndex;
        }
        set
        {
            myForwardRow.dwForwardIfIndex = value;
        }
    }


    public ForwardType ForwrdType
    {
        get
        {
            return myForwardRow.dwForwardType;
        }
        set
        {
            myForwardRow.dwForwardType = value;
        }
    }

    public ForwardProtocol Protocol
    {
        get
        {
            return myForwardRow.dwForwardProto;
        }
        set
        {
            myForwardRow.dwForwardProto = value;
        }
    }


    public int ForwardAge
    {
        get
        {
            return myForwardRow.dwForwardAge;
        }
        set
        {
            myForwardRow.dwForwardAge = value;
        }
    }

    public int ForwardNextHopAS
    {
        get
        {
            return myForwardRow.dwForwardNextHopAS;
        }
        set
        {
            myForwardRow.dwForwardNextHopAS = value;
        }
    }

    public int ForwardMetric1
    {
        get
        {
            return myForwardRow.dwForwardMetric1;
        }
        set
        {
            myForwardRow.dwForwardMetric1 = value;
        }
    }

    public int ForwardMetric2
    {
        get
        {
            return myForwardRow.dwForwardMetric2;
        }
        set
        {
            myForwardRow.dwForwardMetric2 = value;
        }
    }

    public int ForwardMetric3
    {
        get
        {
            return myForwardRow.dwForwardMetric3;
        }
        set
        {
            myForwardRow.dwForwardMetric3 = value;
        }
    }

    public int ForwardMetric4
    {
        get
        {
            return myForwardRow.dwForwardMetric4;
        }
        set
        {
            myForwardRow.dwForwardMetric4 = value;
        }
    }

    public int ForwardMetric5
    {
        get
        {
            return myForwardRow.dwForwardMetric5;
        }
        set
        {
            myForwardRow.dwForwardMetric5 = value;
        }
    }

    #endregion

}
4

2 回答 2

2

GetIpForwardTable 不返回 MIB_IPFORWARDROW 对象的数组,它返回一个 MIB_IPFORWARDTABLE,其中包含行数组数字。所以这至少是一个问题。可能还有其他人,因为这不是用于编组的简单 P/Invoke 集。

值得一提的是,我已经在Smart Device Framework中实现了所有这些代码,特别是在OpenNETCF.Net.NetworkInformation.IPRoutingTable类中

于 2011-01-20T17:05:07.037 回答
0

我不知道该功能是如何工作的,但以下看起来非常可疑。

new MIB_IPFORWARDROW[tableSize / Marshal.SizeOf(typeof(MIB_IPFORWARDROW)) + 1]

你为什么要除以sizeof

于 2011-01-20T17:03:38.167 回答