Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

icmpsendecho (icmp)
 
.
Summary
Sends an ICMP Echo Request and returns any replies. The call returns when the time-out has expired or the reply buffer is filled.

C# Signature:

[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);

VB Signature:

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

User-Defined Types:

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.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Complete "simple sample" coded by Aprelov Sergey:

    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 Int16 DataSize;
            public Int16 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 Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, String requestData, Int16 requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, Int32 replySize, Int32 timeout);

        public bool Ping(IPAddress IP)
        {
            IntPtr ICMPHandle;
            Int32 iIP;
            String sData;
            ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS();
            ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY();
            Int32 iReplies;

            ICMPHandle = IcmpCreateFile();
            iIP = BitConverter.ToInt32(IP.GetAddressBytes(), 0);
            sData = "x";
            oICMPOptions.Ttl = 255;

            iReplies = IcmpSendEcho(ICMPHandle, iIP,
                sData, (Int16) sData.Length, ref oICMPOptions, ref ICMPReply,
                Marshal.SizeOf(ICMPReply), 30);

            IcmpCloseHandle(ICMPHandle);
            if (ICMPReply.Status == 0)
                return true;
            return false;
        }
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

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).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions