locked
Ping for Pocket PC RRS feed

  • Question

  •  

    I want to create a ping like utility for my Pocket PC 2003 device but am unsure where to start. I know that there are free ones out there but I am after the learning experience. Any pointers to get me going in the right direction would be great. I tried the snippet "My.Computer....." isn't liked, so as too alternatives to get started, would be great.

     

    Thanks for any and all help!

    Friday, November 9, 2007 5:16 AM

Answers

  • Hi,

    you'll find a Ping class in the SmartDeviceFramework vorm OpenNETCF. The community edition is for free.

    Cheers, Peter
    Tuesday, November 13, 2007 5:47 AM
  • Hi Stupidav,

    You can P/Invoke IcmpSendEcho2 API to implement ping function. This function sends an ICMP echo request and the call returns either immediately, if the Event parameter is non-NULL, or returns after the specified timeout

    Here is a sample code:

     

               MessageBox.Show(Ping(IPAddress.Loopback).ToString());

                MessageBox.Show(Ping(IPAddress.Parse("192.178.1.2")).ToString());

             

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto )]

            private struct ICMP_OPTIONS

            {

                public Byte Ttl;

                public Byte Tos;

                public Byte Flags;

                public Byte OptionsSize;

                public IntPtr OptionsData;

            };

     

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto )]

            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("iphlpapi.dll", SetLastError = true)]

            private static extern IntPtr IcmpCreateFile();

            [DllImport("iphlpapi.dll", SetLastError = true)]

            private static extern bool IcmpCloseHandle(IntPtr handle);

            [DllImport("iphlpapi.dll", SetLastError = true)]

            private static extern Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, String requestData, Int32 requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, Int32 replySize, Int32 timeout);

     

            public int 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, sData.Length, ref oICMPOptions, ref ICMPReply,

                    Marshal.SizeOf(ICMPReply), 30);

     

                IcmpCloseHandle(ICMPHandle);

                return iReplies;

            }

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, November 13, 2007 6:10 AM

All replies

  • Move the thread from Visual Basic General.

     

    Tuesday, November 13, 2007 5:05 AM
  • Hi,

    you'll find a Ping class in the SmartDeviceFramework vorm OpenNETCF. The community edition is for free.

    Cheers, Peter
    Tuesday, November 13, 2007 5:47 AM
  • Hi Stupidav,

    You can P/Invoke IcmpSendEcho2 API to implement ping function. This function sends an ICMP echo request and the call returns either immediately, if the Event parameter is non-NULL, or returns after the specified timeout

    Here is a sample code:

     

               MessageBox.Show(Ping(IPAddress.Loopback).ToString());

                MessageBox.Show(Ping(IPAddress.Parse("192.178.1.2")).ToString());

             

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto )]

            private struct ICMP_OPTIONS

            {

                public Byte Ttl;

                public Byte Tos;

                public Byte Flags;

                public Byte OptionsSize;

                public IntPtr OptionsData;

            };

     

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto )]

            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("iphlpapi.dll", SetLastError = true)]

            private static extern IntPtr IcmpCreateFile();

            [DllImport("iphlpapi.dll", SetLastError = true)]

            private static extern bool IcmpCloseHandle(IntPtr handle);

            [DllImport("iphlpapi.dll", SetLastError = true)]

            private static extern Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, String requestData, Int32 requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, Int32 replySize, Int32 timeout);

     

            public int 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, sData.Length, ref oICMPOptions, ref ICMPReply,

                    Marshal.SizeOf(ICMPReply), 30);

     

                IcmpCloseHandle(ICMPHandle);

                return iReplies;

            }

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, November 13, 2007 6:10 AM
  •  

    I have implemented the above example and it only works for the loopback (localhost).  Every other IP I tried returns 0.  Is there an issue accessing other public IP addresses?  I tried it from the virtual emulator and I also installed it on my smartphone which has a data plan.

     

    thanks..

    Friday, November 16, 2007 9:58 PM
  •  

    regarding the ping class from OpenNETCF:  It has a bug.  You will see that a timeout is always received on ping.  I tried it even though I read the same issue from many many other users.  Google it.
    Friday, November 16, 2007 10:07 PM
  • hi guang!

    you wouldn't happen to have this in vb code would ya?


    Wednesday, February 13, 2008 2:02 AM
  • Hi bauey,

    There is a lot of site which can convert c# code to vb.net code, like :

    http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

     

    Best regards,

    Guang-Ming Bian - MSFT

    Thursday, February 14, 2008 6:37 AM