Click here to Skip to main content
15,887,135 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 430.9K   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

     
    GeneralRe: ping under XP Pin
    mohammad hajibegloo14-May-04 23:38
    mohammad hajibegloo14-May-04 23:38 
    GeneralRe: ping under XP Pin
    same.wong21-Aug-12 15:13
    same.wong21-Aug-12 15:13 
    GeneralBug Pin
    Yossi Meir15-Mar-03 21:47
    Yossi Meir15-Mar-03 21:47 
    GeneralRe: Bug Pin
    Hanney Wang16-Mar-03 19:08
    Hanney Wang16-Mar-03 19:08 
    GeneralNot work very well - A bug. Pin
    Hanney Wang24-Dec-02 16:36
    Hanney Wang24-Dec-02 16:36 
    GeneralRe: Not work very well - A bug. Pin
    Jesus Oliva1-Feb-03 9:47
    Jesus Oliva1-Feb-03 9:47 
    GeneralRe: Not work very well - A bug. Pin
    NormDroid1-Feb-03 22:14
    professionalNormDroid1-Feb-03 22:14 
    GeneralRe: Not work very well - A bug. Pin
    Hanney Wang15-Mar-03 2:25
    Hanney Wang15-Mar-03 2:25 
    Thank you! But so sorry for the late reply!

    I also realized this problem from Microsoft code sample, so, I
    had found an other way to solve this problem - using MS ICMP APIs. Here is my code:

    #define PING_WINSOCK_VERSION 0x0101
    #define PING_REQ_DATASIZE 64 // Echo Request Data size
    #define PING_ERROR_SUCCESS 0
    #define PING_ERROR_TIMEOUT 1
    #define PING_ERROR_SOCKET 2

    typedef struct tagIPINFO
    {
    unsigned char Ttl; // Time To Live
    unsigned char Tos; // Type Of Service
    unsigned char IPFlags; // IP flags
    unsigned char OptSize; // Size of options data
    unsigned char far * Options; // Options data buffer
    }IPINFO, *PIPINFO;

    typedef struct tagICMPECHO
    {
    unsigned long Source; // Source address
    unsigned long Status; // IP status
    unsigned long RTTime; // Round trip time in milliseconds
    unsigned short DataSize; // Reply data size
    unsigned short Reserved; // Unknown
    void far * pData; // Reply data buffer
    IPINFO ipInfo; // Reply options
    }ICMPECHO, * PICMPECHO;

    // Ping results
    typedef struct tagPINGINFO
    {
    char cHostName[32];
    char cIpAddress[20];
    char cErrorMessage[256];
    unsigned int blTimouted;
    short nSendBytes;
    short nReplyBytes;
    unsigned long nReplyTime;
    unsigned char nReplyTTL;
    }PINGINFO, *PPINGINFO;


    __declspec(dllexport) unsigned int _cdecl PingHost( void far* lpPingInfo )
    {
    PPINGINFO pPingInfo = (PPINGINFO)lpPingInfo;
    if(pPingInfo == NULL)return( FALSE );

    HANDLE (WINAPI *pIcmpCreateFile)(VOID);
    BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
    DWORD (WINAPI *pIcmpSendEcho)
    (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
    HANDLE hndlIcmp; // LoadLibrary() handle to ICMP.DLL
    WSADATA wsaData; // WSADATA
    struct in_addr iaDest; // Internet address structure
    LPHOSTENT pHost; // Pointer to host entry structure
    DWORD *dwAddress; // IP Address
    HANDLE hndlFile; // Handle for IcmpCreateFile()

    // Dynamically load the ICMP.DLL
    hndlIcmp = LoadLibrary("ICMP.DLL");
    if (hndlIcmp == NULL)
    {
    sprintf(pPingInfo->cErrorMessage, "Could not load ICMP.DLL!");
    return( PING_ERROR_SOCKET );
    }
    // Retrieve ICMP function pointers
    pIcmpCreateFile = (HANDLE (WINAPI *)(void))
    GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
    pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
    GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
    pIcmpSendEcho = (DWORD (WINAPI *)
    (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
    GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
    // Check all the function pointers
    if (pIcmpCreateFile == NULL ||
    pIcmpCloseHandle == NULL ||
    pIcmpSendEcho == NULL)
    {
    sprintf(pPingInfo->cErrorMessage, "Error loading ICMP.DLL functions!");
    FreeLibrary((HMODULE)hndlIcmp);
    return( PING_ERROR_SOCKET );
    }

    // Init WinSock
    if (WSAStartup(PING_WINSOCK_VERSION, &wsaData))
    {
    sprintf(pPingInfo->cErrorMessage, "Winsock could not be initialized!");
    WSACleanup();
    return( PING_ERROR_SOCKET );
    }
    // Check WinSock version
    if (0x0101 != wsaData.wVersion)
    {
    sprintf(pPingInfo->cErrorMessage, "No WinSock version 1.1 support found!");
    WSACleanup();
    FreeLibrary((HMODULE)hndlIcmp);
    return( PING_ERROR_SOCKET );
    }
    // Resovling the Host
    iaDest.s_addr = inet_addr(pPingInfo->cHostName);

    if (iaDest.s_addr == INADDR_NONE)
    pHost = gethostbyname(pPingInfo->cHostName);
    else
    pHost = gethostbyaddr((const char *)&iaDest,
    sizeof(struct in_addr), AF_INET);
    if (pHost == NULL)
    {
    sprintf(pPingInfo->cErrorMessage, "Unable resolve host %s!", pPingInfo->cHostName);
    FreeLibrary((HMODULE)hndlIcmp);
    WSACleanup();
    return( PING_ERROR_SOCKET );
    }

    // Copy the IP address
    dwAddress = (DWORD *)(*pHost->h_addr_list);

    // Get an ICMP echo request handle
    hndlFile = pIcmpCreateFile();

    if(hndlFile < 0)
    {
    sprintf(pPingInfo->cErrorMessage, "Unable get ICMP handle!");
    FreeLibrary((HMODULE)hndlIcmp);
    WSACleanup();
    return( PING_ERROR_SOCKET );
    }

    // Start to Ping
    unsigned char request_data[PING_REQ_DATASIZE];
    unsigned char reBuff[sizeof(ICMPECHO)+200];
    IPINFO ipInfo; // IP Options structure
    ICMPECHO *icmpEcho = NULL; // ICMP Echo reply buffer

    memset(reBuff, 0, sizeof(ICMPECHO)+200);
    memset(request_data, 0, sizeof(request_data));
    memset(&ipInfo, 0, sizeof(ipInfo));
    ipInfo.Ttl = 255;
    ipInfo.Options = request_data;

    pIcmpSendEcho(
    hndlFile, // Handle from IcmpCreateFile()
    *dwAddress, // Destination IP address
    request_data, // Pointer to buffer to send
    sizeof(request_data), // Size of buffer in bytes
    &ipInfo, // Request options
    reBuff, // Reply buffer
    sizeof(reBuff),
    1000); // Time to wait in milliseconds

    icmpEcho = (ICMPECHO *) reBuff;
    // Get the results
    iaDest.s_addr = icmpEcho->Source;
    sprintf(pPingInfo->cIpAddress, "%s", inet_ntoa(iaDest));
    if (icmpEcho->Status)
    {
    sprintf(pPingInfo->cErrorMessage, "%d - Request timed out.", icmpEcho->Status);
    pPingInfo->blTimouted = TRUE;
    pPingInfo->nReplyBytes = pPingInfo->nReplyTTL = 0;
    pPingInfo->nReplyTime = 0;
    }
    else
    {
    sprintf(pPingInfo->cErrorMessage, "No Error!");
    pPingInfo->blTimouted = FALSE;
    pPingInfo->nReplyBytes = icmpEcho->DataSize;
    pPingInfo->nReplyTTL = icmpEcho->ipInfo.Ttl;
    pPingInfo->nReplyTime = icmpEcho->RTTime;
    }

    // Close the echo request file handle
    pIcmpCloseHandle(hndlFile);
    FreeLibrary((HMODULE)hndlIcmp);
    WSACleanup();

    return pPingInfo->blTimouted ? PING_ERROR_TIMEOUT : PING_ERROR_SUCCESS ;
    }



    Regards,
    Hanney
    GeneralRe: Not work very well - A bug - for Sonu Kapoor . Pin
    Hanney Wang30-Mar-03 15:36
    Hanney Wang30-Mar-03 15:36 
    GeneralRe: Not work very well - A bug - for Sonu Kapoor . Pin
    Sonu Kapoor1-Apr-03 0:39
    Sonu Kapoor1-Apr-03 0:39 
    GeneralRe: Not work very well - A bug - for Sonu Kapoor . Pin
    Yin Gang21-Sep-05 21:32
    Yin Gang21-Sep-05 21:32 
    QuestionCould you give me a TIP? Pin
    one_eddie22-Nov-02 12:45
    one_eddie22-Nov-02 12:45 
    Generalurgent !!!!! Pin
    balajiParthasarathy19-Aug-02 3:46
    sussbalajiParthasarathy19-Aug-02 3:46 
    GeneralRe: urgent !!!!! Pin
    Daniel 'Tak' M.19-Oct-02 6:43
    Daniel 'Tak' M.19-Oct-02 6:43 
    GeneralFinding local IP Pin
    Camran3-Jul-02 18:05
    Camran3-Jul-02 18:05 
    GeneralRe: Finding local IP Pin
    3-Jul-02 21:11
    suss3-Jul-02 21:11 
    GeneralRe: Finding local IP Pin
    3-Jul-02 21:23
    suss3-Jul-02 21:23 
    GeneralRe: Finding local IP Pin
    6-Jul-02 15:22
    suss6-Jul-02 15:22 
    GeneralNeed Help!!!! Pin
    19-May-02 13:12
    suss19-May-02 13:12 
    GeneralRe: Need Help!!!! Pin
    NormDroid3-Jul-02 21:14
    professionalNormDroid3-Jul-02 21:14 
    QuestionWin2K/NT4 only as administrator ??? Pin
    Olli22-Apr-02 23:59
    Olli22-Apr-02 23:59 
    AnswerRe: Win2K/NT4 only as administrator ??? Pin
    Robert W.9-Jul-03 7:39
    Robert W.9-Jul-03 7:39 
    GeneralRe: Win2K/NT4 only as administrator ??? Pin
    borini11-Feb-04 5:43
    borini11-Feb-04 5:43 
    GeneralRe: Win2K/NT4 only as administrator ??? Pin
    Laszlo Kiss12-Apr-05 23:06
    Laszlo Kiss12-Apr-05 23:06 
    GeneralRe: Win2K/NT4 only as administrator ??? Pin
    zghelp15-Nov-04 3:55
    zghelp15-Nov-04 3:55 

    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.