Question on using C# IsDestinationReachable function

Posted on 2006-04-06
Last Modified: 2012-06-27
Hi, I am an SMS administrator for our Company. It seems on a daily basis I have to connect to computers to fix something related to SMS. Well we have a large remote user base and frequently those machines are not connected when I need to do something to them. So I suppose I could try to remember to ping them throughout the day or set up a ping -t command, but that seemed wasteful. So as a beginning c# hobbyist (won't say programmer just yet), I broke out my Visual C# Express 2005 and thought how could I automate this task of finding out when a computer comes online? So I found out about the function IsDestinationReachable using a DLL Import within c#.

I set up a window to ask for a computer name, then pass that computer name to the next window that does the actual checking. If it does not reach the given machine, it sleeps for 30 seconds then tries again. Once the machine is reached, the window pops up on the display and an audible tone is also generated.

Well this seems to work for most machines. However, I have seen this app tell me a machine is now online when it is sitting right next to me, turned off, and unplugged from the network. If I try to ping the machine, I get no reply, as I would expect.

I have listed my code below. Can anyone offer some thoughts, hints, etc.?
        private void frmWaitForActive_Load(object sender, EventArgs e)
            //set window title and hide window.
            Text = "Is " + strCompName + " online?";

            /*This section of code will perform the actual test of
             * determining whether the indicated computer is reachable.
             * A boolean variable is initially set to false, then the
             * function called IsDestinationReachable is run using the
             * supplied computer name as the host.
             * If the host is reachable, then a window will display
             * for the user indicating such, as well as a tone will sound
             * as an audible indicator.
             * If the host is not found, then the program sleeps
             * for 30 seconds (30000 milliseconds) and the process
             * is then repeated to check if the host is reachable.
            bool blnFlag = false;
            while (blnFlag == false)
                if (IsDestinationReachable(strCompName, IntPtr.Zero))
                    //the host is reachable. Now notify the administrator
                    WindowState = FormWindowState.Normal;
                    txtCompName.Text = strCompName + " is now on-line!";
                    blnFlag = true;
                    Beep(500, 550);
                    //Sleep 30 seconds (30000 milliseconds)

Thanks for any ideas.
Question by:MichaelCohoon
    LVL 96

    Accepted Solution


    IsDestinationReachable uses a Ping to check the status, so I fail to see what you are saving by using this API call.


    Author Comment

    Bob, I think I can do the code to start a ping command, using the Process.Start, but I really don't have a clue how I would be able to determine if I receive a response or not via code. Would that check be something easily done?


    Author Comment

    Bob, you gave me an idea to go the way of Ping. So a search on google for "C# ping status" turned up code for me. .Net 2.0 now includes network functions that are relatively easy to use, including a ping. So I will go that route with my program.

    I thank you for the idea.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now