Click here to Skip to main content
15,888,092 members
Articles / Desktop Programming / MFC
Article

Ping for Windows

Rate me:
Please Sign up or sign in to vote.
4.33/5 (38 votes)
15 Feb 2001 431.1K   21.9K   72   84
A simple Windows based ping program
  • Download source files - 133 Kb
  • Download executable - 108 Kb
  • Sample Image - WinPing.jpg

    Introduction

    This is an extremely simple Ping program for Windows. I'm using Chris Maunder's auto-completion combo box class and there's code borrowed from a Winsock 2.0 book. Credits are mentioned on the About box.

    Please see the code for details - it's quite simple.

    History

    7 Sep 2000:

    • Fixes to MRU Combo
    • Fixes to Duplicate host/ip addresses
    • Fixes to Ping times (Now correct)

    16 Sep 2001:

    • Updated source code

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior) Software Kinetics
    United Kingdom United Kingdom




    Software Kinetics
    are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


    We specialise in:

    • User Interface Design
    • Desktop Development
    • Windows Phone Development
    • Windows Presentation Framework
    • Windows Forms
    • Windows Communication Framework
    • Windows Services
    • Network Applications
    • Database Applications
    • Web Development
    • Web Services
    • Silverlight
    • ASP.net


    Visit Software Kinetics

    Comments and Discussions

     
    QuestionLicense? Pin
    Member 132538641-Jan-19 19:40
    Member 132538641-Jan-19 19:40 
    QuestionIt's pinging a virtual machine on VMWare when I pause that Pin
    cnalmeida14-Oct-15 3:51
    cnalmeida14-Oct-15 3:51 
    QuestionUpdates for vs2013 - Continued Pin
    rhfritz19-Sep-15 8:49
    rhfritz19-Sep-15 8:49 
    SuggestionUpdates for vs2013 Pin
    rhfritz19-Sep-15 8:43
    rhfritz19-Sep-15 8:43 
    I've made the following updates to support building in vs2013. Since I can't upload files, I won't post the project files. But the project itself converts fine. Primarily I cleared compiler warnings. Then I added some code to display the error message string when the calls didn't work (usually because a 'user' account was running the app). Then I had to fix a problem that the H-scroll bar did not appear when the error text was longer than the control.

    stdafx.h:
    C++
    // stdafx.h : include file for standard system include files,
    //  or project specific include files that are used frequently, but
    //      are changed infrequently
    //
    
    #if !defined(AFX_STDAFX_H__6609025C_5D2A_4866_A906_EECB83458979__INCLUDED_)
    #define AFX_STDAFX_H__6609025C_5D2A_4866_A906_EECB83458979__INCLUDED_
    
    #pragma once
    
    #define VC_EXTRALEAN					// Exclude rarely-used stuff from Windows headers
    #define WIN32_LEAN_AND_MEAN				// Exclude rarely-used stuff from Windows headers
    #define NO_WARN_MBCS_MFC_DEPRECATION	// Eliminates: vs2013 warning C4996: 'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated and may be removed in a future version of MFC
    #define _WINSOCK_DEPRECATED_NO_WARNINGS	// Eliminates: vs2013 warning: 'Use getaddrinfo() or GetAddrInfoW() instead ... this is about old calls only supporting IPV4 and not IPV6
    
    #if _MSC_VER > 1300
    #pragma warning( disable : 4996 )		// VS .NET 2005 deprecation warning C4996: 'xxxx': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _xxxx
    #define strcmpi _strcmpi	// warning C4996: 'strcmpi': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strcmpi.
    #ifndef	_CRT_SECURE_NO_WARNINGS	
    #undef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
    #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1	 	// VS .NET 2005 Define safe prototype
    #define _CRT_SECURE_NO_WARNINGS	1
    #endif
    #endif // _MSC_VER > 1300
    
    #ifndef _WIN32_WINNT		// Allow use of latest features 
    #define _WIN32_WINNT _WIN32_WINNT_MAXVER		// This clears a warning and is the default
    #endif						
    
    #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS	// some CString constructors will be explicit
    
    #define VC_EXTRALEAN					// Exclude rarely-used stuff from Windows headers
    #define WIN32_LEAN_AND_MEAN				// Exclude rarely-used stuff from Windows headers
    #define NO_WARN_MBCS_MFC_DEPRECATION	// Eliminates: vs2013 warning C4996: 'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated and may be removed in a future version of MFC
    
    // turns off MFC's hiding of some common and often safely ignored warning messages
    #define _AFX_ALL_WARNINGS
    
    #include <afxwin.h>         // MFC core and standard components
    #include <afxext.h>         // MFC extensions
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h>			// MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT
    
    #include <afxsock.h>		// MFC socket extensions
    
    #define WM_MSG_STATUS	WM_USER + 0x0100
    #define WM_PING_END		WM_USER + 0x0101
    
    // string message allocator for posting messages between windows...
    static char* AllocBuffer(CString strMsg)
    {
    	int nLen = strMsg.GetLength();
    	char *pBuffer = new char[nLen+1]; 
    	
    	strcpy(pBuffer,(const char*)strMsg);
    
    	ASSERT(pBuffer != NULL);
    	return pBuffer;
    }
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_STDAFX_H__6609025C_5D2A_4866_A906_EECB83458979__INCLUDED_)


    WinPing.cpp
    C++
    // WinPing.cpp : Defines the class behaviors for the application.
    //
    
    #include "stdafx.h"
    #include "WinPing.h"
    #include "WinPingDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CWinPingApp
    
    BEGIN_MESSAGE_MAP(CWinPingApp, CWinApp)
    	//{{AFX_MSG_MAP(CWinPingApp)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    		//    DO NOT EDIT what you see in these blocks of generated code!
    	//}}AFX_MSG
    	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CWinPingApp construction
    
    CWinPingApp::CWinPingApp()
    {
    	// TODO: add construction code here,
    	// Place all significant initialization in InitInstance
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // The one and only CWinPingApp object
    
    CWinPingApp theApp;
    
    /////////////////////////////////////////////////////////////////////////////
    // CWinPingApp initialization
    
    BOOL CWinPingApp::InitInstance()
    {
    	if (!AfxSocketInit())
    	{
    		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
    		return FALSE;
    	}
    
    	//AfxEnableControlContainer();
    
    	SetRegistryKey("FSWH");
    
    	// Standard initialization
    //	// If you are not using these features and wish to reduce the size
    //	//  of your final executable, you should remove from the following
    //	//  the specific initialization routines you do not need.
    //
    //#ifdef _AFXDLL
    //	Enable3dControls();			// Call this when using MFC in a shared DLL
    //#else
    //	Enable3dControlsStatic();	// Call this when linking to MFC statically
    //#endif
    
    	CWinPingDlg dlg;
    	m_pMainWnd = &dlg;
    	int nResponse = dlg.DoModal();
    	if (nResponse == IDOK)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with OK
    	}
    	else if (nResponse == IDCANCEL)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with Cancel
    	}
    
    	// Since the dialog has been closed, return FALSE so that we exit the
    	//  application, rather than start the application's message pump.
    	return FALSE;
    }


    Added the following to Ping.h:
    C++
    protected:
    	void WSAError(LPCSTR pstrFrom); // Existing
    	CString GetLastErrorDescription(DWORD dwErrorCode);


    Added / Modified in Ping.cpp :
    C++
    CString CPing::GetLastErrorDescription(DWORD dwErrorCode)
    {
    	CString csErrorDescription;
    
    	LPVOID lpMsgBuf;
    	if (FormatMessage(
    		FORMAT_MESSAGE_ALLOCATE_BUFFER |
    		FORMAT_MESSAGE_FROM_SYSTEM |
    		FORMAT_MESSAGE_IGNORE_INSERTS,
    		NULL,
    		dwErrorCode,
    		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    		(LPTSTR)&lpMsgBuf,
    		0,
    		NULL))
    	{
    		csErrorDescription.Format("WSAError[%d] : %s", dwErrorCode, (char *)lpMsgBuf);
    
    		LocalFree(lpMsgBuf);
    	}
    
    	return csErrorDescription;
    }
    
    void CPing::WSAError(LPCSTR lpMsg)
    {
    	CString strMsg;
    	DWORD dwErrorCode = WSAGetLastError();
    
    	strMsg.Format("%s - %s", lpMsg, GetLastErrorDescription(dwErrorCode));
    
    	::PostMessage(m_hWnd,WM_MSG_STATUS, 0, (LPARAM) AllocBuffer(strMsg));
    }

    QuestionPing Pin
    Member 1170006121-May-15 9:28
    Member 1170006121-May-15 9:28 
    QuestionAwesome, thank you! Pin
    J Eakin21-Mar-13 8:35
    J Eakin21-Mar-13 8:35 
    Generalyou are a hero ! man Pin
    batsword4-Sep-10 19:17
    batsword4-Sep-10 19:17 
    GeneralBug Pin
    StKgn22-Apr-10 0:12
    StKgn22-Apr-10 0:12 
    GeneralRe: Bug Pin
    NormDroid22-Apr-10 0:28
    professionalNormDroid22-Apr-10 0:28 
    GeneralNon administrator account Pin
    saenz tomas24-Mar-10 7:08
    saenz tomas24-Mar-10 7:08 
    GeneralMy vote of 1 Pin
    Sheetal_Joshi8-Dec-09 6:10
    Sheetal_Joshi8-Dec-09 6:10 
    Generalit is not a real multi-thread ? [modified] Pin
    AhmedOsamaMoh4-Dec-08 10:51
    AhmedOsamaMoh4-Dec-08 10:51 
    GeneralNot working on Vista Pin
    TRK29-Oct-07 9:35
    TRK29-Oct-07 9:35 
    GeneralRe: Not working on Vista Pin
    Member 446097211-Mar-08 4:30
    Member 446097211-Mar-08 4:30 
    GeneralRe: Not working on Vista Pin
    ashishbhadiyadra13-Apr-09 19:14
    ashishbhadiyadra13-Apr-09 19:14 
    GeneralRe: Not working on Vista Pin
    cniculescu23-Sep-09 10:21
    cniculescu23-Sep-09 10:21 
    GeneralRe: Not working on Vista Pin
    liugh69819-Feb-10 22:45
    liugh69819-Feb-10 22:45 
    GeneralRe: Not working on Vista Pin
    steveh211212-Jul-14 22:24
    steveh211212-Jul-14 22:24 
    GeneralRe: Not working on Vista Pin
    zhangchunming11-Mar-12 1:35
    zhangchunming11-Mar-12 1:35 
    GeneralExcellent work! Pin
    428820-Aug-07 10:35
    428820-Aug-07 10:35 
    GeneralCalling OnTimer(); after calling socket( Pin
    marreoragarn10-May-07 5:33
    marreoragarn10-May-07 5:33 
    Generalwhen my firewall is open this ping program cannot ping to the web server Pin
    sude6-Dec-06 16:10
    sude6-Dec-06 16:10 
    GeneralRequest Pin
    rahul_s99915-Jun-06 14:30
    rahul_s99915-Jun-06 14:30 
    GeneralRange maintaining Pin
    0pal13-Apr-06 21:10
    0pal13-Apr-06 21:10 
    GeneralRe: Range maintaining Pin
    webmastersitesi5-Jan-09 18:15
    webmastersitesi5-Jan-09 18:15 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.