CPing -- Easily ping a host from Windows 95 and Windows NT

How do I use it?
The CPing::Ping() Member Function
Important things to remember
Little Q 'n' A

What does it do?

    You may not know it, but there is a command built into windows called "ping". You can go to a dos prompt while you're connected to the Internet and type:
    
    ping www.codeguru.com
    
    This command lets you know weather or not that host is up and running. This code gives you that functionality form inside your program. I found a couple of other PING or ICMP articles on codeguru.com, but I failed to get any of them to work under '95. Finding a solution that worked for both 'NT and '95 was hard. After I found it, I figured I'd share it; so I packaged it up into my CPing class.
    Note: None of the actual ping code is mine. My code is just the class wrapper. If I knew who wrote it, I would give him or her full credit. If anyone knows, drop me a line.

How do I use it?

  • Create your MFC project
      Don't tell the app wizard that you want to use Windows Sockets
      Don't #include afxsock.h
      Don't call AfxSocketInit()
  • Copy CPing.cpp and CPing.h into your project directory
  • Insert CPing.cpp into your project
  • In the C++ settings of your project settings remove /Yu"stdafx.h" from project options in both release and debug
  • #include <winsock.h> in your stdafx.h
  • In the linker tab, add WSOCK32.LIB
  • #include "CPing.h" in all the files you wish to reference the CPing class or any objects made from it
  • Construct a CPing object and call the Ping() member function

The CPing::Ping() Member Function

    CPing::Ping

      BOOL Ping( char* strHost );

      Return Value

      Nonzero if the host specified by strHost responds; otherwise 0.

      Parameters

      strHost   A pointer to the null-terminated name or IP Address of the host to ping. If strHost points to an IP Address, it must be in the "XXX.XXX.XXX.XXX" format.

      Remarks

      If the host name specified by strHost cannot be resolved, the function returns 0. A return value of 0 can also mean the host failed to respond.

      Example

      The following example demonstrates the use of CPing::Ping.

      
      // example for CPing::Ping
      
      CPing ping;
      
      //ping the gateway
      BOOL bResult = ping.Ping("192.168.0.254");
      
      if( bResult )
      {
      	//the gateway is working fine
      	//lets ping a couple of sites to see if we can get out.
      	if(ping.Ping("www.codeguru.com") || ping.Ping("www.netscape.com"))
      	{
      		//works just fine
      		AfxMessageBox("Everything is A OK.");
      	}
      	else
      	{
      		//DNS error or we can't get out.
      		AfxMessageBox("Can't ping outside the subnet.  It may be a DNS problem or the ISP's fault.");
      	}
      }
      else
      {
      	//the gateway must be down
      	AfxMessageBox("The gateway or router is down.");
      }
      

Important things to remember

  • This class uses ICMP.DLL. Microsoft wasn't too hot on the idea of making ICMP.DLL. They plan to drop support of it. I don't know if it exists in Windows 98. I don't know if it exists in some versions of '95. I suggest your setup program check for ICMP.DLL and install it in the program directory as needed.

  • This class dynamically loads ICMP.DLL in the constructor. So don't make multiple CPing objects!

  • This class also calls WSAStartup(); in the constructor and WSACleanup(); in the destructor. So don't make multiple CPing objects! Also, If your application calls WSAStartup() and WSACleanup(), I suggest you remark out WSAStartup() and WSACleanup() in my constructor and destructor.

  • Since my class does so much stuff in the constructor and destructor, be very wise about where you declare your CPing object. For example, don't declare it in the middle of a while or for loop. It will constantly construct and destruct CPing objects. The icmp.dll will be popping in and out of memory. It will be starting and stopping Winsock services. Not a good idea. I suggest you place it in your project in a way that it will be constructed and destructed only once.

Little Q 'n' A

    Q. Does it compile under warning level 4?
    A. Yes.

    Q. Will it compile under UNICODE settings?
    A. I don't know. I've never used UNICODE.

    Q. What version of Visual C++ did you use?
    A. Microsoft Visual C++ 5.0

    Q. Will it compile under newer or older versions of Visual C++?
    A. It should compile on version 4.0 and above, but I haven't tested it.

    Q. What version of MFC was this code built with?
    A. Whatever comes with Visual C++ 5.0

    Q. Is the code const correct?
    A. I have no clue what that is. I assume no.

    Q. Does it work under '98?
    A. I don't know. Microsoft may have ditched ICMP.DLL or maybe the kept it for backward compatibility. You may end up having to place ICMP.DLL in the same path as the .exe to get it to work.

    Q. Do I have to use MFC?
    A. No. I didn't reference any of the MFC classes or objects. If you wish to not use MFC, you'll will have to create a StdAfx.h file or take the #include "StdAfx.h" out of my class. You will have to #include <windows.h> and #include <winsock.h>. Even after that, you may have to monkey around with it a bit to get it to work.

Download demo executable file - 67.2 KB

Download demo project - 28.6 KB

Download source - 1.93 KB