删除 Windows CE 6.0 CF.NET 2.0 上的 ARP 条目

Delete ARP entry on Windows CE 6.0 CF.NET 2.0

我必须解决的任务是向目标设备发送 ARP 请求。

所以我开始在 C# 中 P/Invoke SendARP - CompactFramework.NET 2.0 for Windows CE 6.0.

[DllImport("iphlpapi.dll")]
private static extern int SendARP(uint destIp, uint srcIp, [Out] byte[] pMacAddr, ref int phyAddrLen);

在认识到本地 ARP 缓存用于请求 IP 的已存在条目后,我想手动删除该条目。

所以我开始使用来自

的以下代码获取IPNetTable

我最终成功加载了 MIB_IPNETROW,但是当我调用

[DllImport("iphlpapi.dll")]
private static extern int DeleteIpNetEntry(MIB_IPNETROW pArpEntry);

我收到 ERROR_INVALID_PARAMETER 异常(代码 87)。

这是我的结构:(我测试了它的两个版本)

[StructLayout(LayoutKind.Sequential)]
internal struct MIB_IPNETROW
{
    [MarshalAs(UnmanagedType.U4)] public int dwIndex;
    [MarshalAs(UnmanagedType.U4)] public int dwPhysAddrLen;
    [MarshalAs(UnmanagedType.U1)] public byte mac0;
    [MarshalAs(UnmanagedType.U1)] public byte mac1;
    [MarshalAs(UnmanagedType.U1)] public byte mac2;
    [MarshalAs(UnmanagedType.U1)] public byte mac3;
    [MarshalAs(UnmanagedType.U1)] public byte mac4;
    [MarshalAs(UnmanagedType.U1)] public byte mac5;
    [MarshalAs(UnmanagedType.U1)] public byte mac6;
    [MarshalAs(UnmanagedType.U1)] public byte mac7;
    [MarshalAs(UnmanagedType.U4)] public int dwAddr;
    [MarshalAs(UnmanagedType.U4)] public int dwType;
}

//[StructLayout(LayoutKind.Sequential)]
//internal struct MIB_IPNETROW
//{
//    [MarshalAs(UnmanagedType.U4)] public int dwIndex;
//    [MarshalAs(UnmanagedType.U4)] public int dwPhysAddrLen;
//    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] bPhysAddr;
//    [MarshalAs(UnmanagedType.U4)] public int dwAddr;
//    [MarshalAs(UnmanagedType.U4)] public int dwType;
//}

最后但同样重要的是我的功能:

    private static void DeleteArpCache(string dstAddress, bool flush)
    {
        if (!string.IsNullOrEmpty(dstAddress))
        {
            IPAddress ipAddress = IPAddress.Parse(dstAddress);
            byte[] destination = ipAddress.GetAddressBytes();

            Log.DebugFormat("Try to get MIB_IPNETROW for IP [{0}]", dstAddress);
            MIB_IPNETROW? rowValue = GetRowByIpNetTable(destination);
            if (rowValue != null)
            {
                MIB_IPNETROW row = (MIB_IPNETROW) rowValue;

                if (flush)
                {
                    Log.DebugFormat("Try to delete ARP entries for Adapter Index {0}", row.dwIndex);
                    int result = FlushIpNetTable(row.dwIndex);
                    if (result == 0)
                    {
                        Log.Info("Deleted ARP entries successfully");
                    }
                    else
                    {
                        Log.ErrorFormat("Delete ARP entries failed with Code {0} for destination [{1}]", result,
                            dstAddress);
                    }
                }
                else
                {
                    Log.DebugFormat("Try to delete single ARP entry for IP [{0}]", dstAddress);
                    int result = DeleteIpNetEntry(row);
                    if (result == 0)
                    {
                        Log.Info("Deleted ARP entry successfully");
                    }
                    else
                    {
                        Log.ErrorFormat("Delete ARP entry failed with Code {0} for destination [{1}]", result,
                            dstAddress);
                    }
                }
            }
        }
    }

FlushIpNetTable 很有魅力! 那么我的 DeleteIpNetEntry 参数不匹配的原因可能是什么? 有人遇到过类似的问题吗?

亲切的问候!

确实如此! josef 是对的,这个参数是 ref ...

[DllImport("iphlpapi.dll")]
private static extern int DeleteIpNetEntry(ref MIB_IPNETROW pArpEntry);

非常感谢!