the task I had to solve was to send an ARP request to a target device.
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);
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.
So I started to acquire the IPNetTable by using the following code from https://stackoverflow.com/a/1148861/3635715
I ended up to load my MIB_IPNETROW successfully, but when I call
[DllImport("iphlpapi.dll")]
private static extern int DeleteIpNetEntry(MIB_IPNETROW pArpEntry);
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 works like a charm! So what could be the reason of my parameter mismatch at DeleteIpNetEntry? Has someone had a similar problem?
Kind regards!
Indeed! josef is right, this param is ref ...
Thank you so much!