Click here to Skip to main content
15,886,857 members

pinging windows 64 bit with ICMP dosn't work ?

EgyptianRobot asked:

Open original thread
this class work excellent with 32 bit but dosn't with 64 bit

any body can help me to fix it ?


C#
using System;
using System.Net;
using System.Runtime.InteropServices;

namespace testPing
{
    class Class1
    {
        public static class ServerStatus
        {

            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct ICMP_ECHO_REPLY
            {
                public uint Address;
                public uint Status;
                public uint RoundTripTime;
                public ushort DataSize;
                public ushort Reserved;
                public IntPtr Data;
                public IP_OPTION_INFORMATION Options;
            }

            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct IP_OPTION_INFORMATION
            {
                public byte TTL;
                public byte TOS;
                public byte Flags;
                public byte OptionsSize;
                public IntPtr OptionsData;
                public int RealOptionData;
            }

            public static class Icmp
            {
                public const int IP_SUCCESS = 0;
                public const int IP_BUF_TOO_SMALL = 11001;
                public const int IP_REQ_TIMED_OUT = 11010;
                [DllImport("icmp.dll")]
                public static extern IntPtr IcmpCreateFile();
                [DllImport("icmp.dll")]
                public static extern uint IcmpSendEcho(IntPtr icmpHandle, uint
                ipAddr, ref int requestData, ushort requestSize, IntPtr optionInfo, ref
ICMP_ECHO_REPLY replyBuffer, uint replySize, int timeout);
                [DllImport("icmp.dll")]
                public static extern bool IcmpCloseHandle(IntPtr icmpHandle);
                public static bool Ping(string host)
                {
                    uint addr =
                    BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0);
                    IntPtr h = IcmpCreateFile();
                    int req = 123456789;
                    ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY();
                    uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero,
                    ref rep, 56, 10);
                    IcmpCloseHandle(h);
                    return (retval != 0 && rep.Status == IP_SUCCESS);
                }
            }
        }

    }
}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900