[DllImport("icmp.dll", SetLastError=true)]
static extern Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, IntPtr requestData, Int16 requestSize, IntPtr requestOptions, IntPtr replyBuffer, Int32 replySize, Int32 timeout);
Declare Function IcmpSendEcho Lib "icmp.dll" (icmpHandle as IntPtr, destinationAddress as Int32, requestData as IntPtr, requestSize as Int16, requestOptions as IntPtr, replyBuffer as IntPtr, replySize as Int32, timeout as Int32) As Int32
requestOptions is an IP_OPTION_INFORMATION, or IntPtr.Zero.
replyBuffer receives an ICMP_ECHO_REPLY followed by zero or more bytes of reply data for each reply received.
None.
Please add some!
Complete "simple sample" coded by Aprelov Sergey:
using System;
using System.Net;
using System.Runtime.InteropServices;
public class IcmpPing
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct ICMP_OPTIONS
{
public byte Ttl;
public byte Tos;
public byte Flags;
public byte OptionsSize;
public IntPtr OptionsData;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct ICMP_ECHO_REPLY
{
public int Address;
public int Status;
public int RoundTripTime;
public short DataSize;
public short Reserved;
public IntPtr DataPtr;
public ICMP_OPTIONS Options;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=250)]
public string Data;
}
[DllImport("icmp.dll", SetLastError=true)]
private static extern IntPtr IcmpCreateFile();
[DllImport("icmp.dll", SetLastError=true)]
private static extern bool IcmpCloseHandle(IntPtr handle);
[DllImport("icmp.dll", SetLastError=true)]
private static extern int IcmpSendEcho(IntPtr icmpHandle, int destinationAddress, string requestData, short requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, int replySize, int timeout);
public bool Ping(IPAddress ip)
{
IntPtr icmpHandle = IcmpCreateFile();
ICMP_OPTIONS icmpOptions = new ICMP_OPTIONS();
icmpOptions.Ttl = 255;
ICMP_ECHO_REPLY icmpReply = new ICMP_ECHO_REPLY();
string sData = "x";
int iReplies = IcmpSendEcho(icmpHandle, BitConverter.ToInt32(ip.GetAddressBytes(), 0), sData, (short)sData.Length, ref icmpOptions, ref icmpReply, Marshal.SizeOf(icmpReply), 30);
IcmpCloseHandle(icmpHandle);
if (icmpReply.Status == 0)
return true;
return false;
}
}