问题描述
我要解决的任务是向目标设备发送ARP请求.
the task I had to solve was to send an ARP request to a target device.
所以我开始在C#-Windows CE 6.0的CompactFramework.NET 2.0中P/调用SendARP.
So I started to P/Invoke SendARP in C# - 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);
认识到在所请求IP的条目已经存在的情况下使用了本地ARP缓存,我想手动删除此条目.
After recognising that the local ARP cache is used in case of an already existing entry of the requested IP I wanted to delete this entry manually.
因此,我开始使用来自 https://stackoverflow.com/a/1148861/3635715
So I started to acquire the IPNetTable by using the following code from https://stackoverflow.com/a/1148861/3635715
我最终成功地加载了MIB_IPNETROW,但是当我打电话时
I ended up to load my MIB_IPNETROW successfully, but when I call
[DllImport("iphlpapi.dll")]
private static extern int DeleteIpNetEntry(MIB_IPNETROW pArpEntry);
我收到ERROR_INVALID_PARAMETER异常(代码87).
I get an ERROR_INVALID_PARAMETER exception (code 87).
这是我的结构:(我测试了它的两个版本)
This is my struct: (I tested two versions of it)
[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;
//}
最后但并非最不重要的功能:
And last but not least my function:
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的参数不匹配的原因可能是什么?有人有类似的问题吗?
FlushIpNetTable works like a charm!So what could be the reason of my parameter mismatch at DeleteIpNetEntry?Has someone had a similar problem?
亲切的问候!
推荐答案
确实! josef是对的,这个参数是ref ...
Indeed! josef is right, this param is ref ...
[DllImport("iphlpapi.dll")]
private static extern int DeleteIpNetEntry(ref MIB_IPNETROW pArpEntry);
非常感谢您!
这篇关于在Windows CE 6.0 CF.NET 2.0上删除ARP条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!