Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than icmp, prefix the name with the module name and a period.
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
replyBuffer receives an ICMP_ECHO_REPLY followed by zero or more bytes of reply data for each reply received.
Notes:
None.
Tips & Tricks:
Please add some!
Sample Code:
Complete "simple samplehttps://www.pinvoke.net/emoticons/regular_smile.gif" 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";
// still not quite sure what the times are, e.g., the max time can be set at
// 5 mSec and we successfully complete the ping in 188 mSec... should timeout.
// for tinyc, pull definitions out of cygwin /usr/include/w32api/...
#ifdef __TINYC__
// _ming.h:
#ifndef __LP64__ /* 32 bit target, 64 bit Mingw target */
#define __LONG32 long
#else /* 64 bit Cygwin target */
#define __LONG32 int
#endif
// != 0 can still be dest unreachable, test status further down too
if (dwRetVal == 0) {
printf("No reply from %s after %d mSec\n", ip_addr_str, maxMsec);
return 1;
}
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
if (0 != pEchoReply->Status) {
printf("no reply from %s after %d mSec\n", ip_addr_str, maxMsec);
return 1;
}
printf("Roundtrip time to %s = %ld mSec\n", ip_addr_str,
pEchoReply->RoundTripTime);
IcmpCloseHandle(hIcmpFile);
return 0;
}
Contributed by John Refling
Describes the options to be included in the header of an IP packet.
3/16/2007 8:17:22 AM - anonymous
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Describes the data returned in response to an echo request.
3/16/2007 8:16:56 AM - anonymous
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).