gpt4 book ai didi

c# - 在 Windows CE 6.0 CF.NET 2.0 上删除 ARP 条目

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:01 25 4
gpt4 key购买 nike

我必须解决的任务是向目标设备发送 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 的已存在条目后,我想手动删除该条目。

所以我开始使用来自 https://stackoverflow.com/a/1148861/3635715 的以下代码获取 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 参数不匹配的原因可能是什么?有人遇到过类似的问题吗?

亲切的问候!

最佳答案

确实如此!约瑟夫是对的,这个参数是 ref ...

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

非常感谢!

关于c# - 在 Windows CE 6.0 CF.NET 2.0 上删除 ARP 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com