HB_PING() under Harbour

HB_PING() under Harbour

Postby Natter » Thu Nov 25, 2021 10:56 am

The HB_PING() function works fine under xHarbour, but I can't compile under Harbor. What could be the reason ?
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: HB_PING() under Harbour

Postby karinha » Thu Nov 25, 2021 12:42 pm

Code: Select all  Expand view  RUN

// \samples\HBPING.PRG - 25/11/2021 - kapiabafwh@gmail.com

#include "FiveWin.ch"
#include "HbCompat.ch"

STATIC oWnd

FUNCTION Main()

   LOCAL oBar, cTitle, nRet

   cTitle := "HB_PING IN HARBOUR/(x)HARBOUR"

   DEFINE WINDOW oWnd TITLE cTitle

   DEFINE BUTTONBAR oBar _3D OF oWnd

   DEFINE BUTTON OF oBar ACTION My_Ping_HB()

   SET MESSAGE OF oWnd TO cTitle NOINSET CLOCK DATE KEYBOARD

   ACTIVATE WINDOW oWnd MAXIMIZED

RETURN NIL

FUNCTION My_Ping_HB()

   // LOCAL xErrorPing, xUrl := "www.google.com"
   LOCAL xErrorPing, xUrl := "www.fivewin.com.br"

   xErrorPing := HB_PING( xUrl )

   DO CASE
   CASE xErrorPing = 0
      MsgInfo( "Ping to " + xUrl + " Succesful", "Internet Connection Available" )
   CASE xErrorPing = 11001
      MsgInfo( "The reply buffer was too small." )
   CASE xErrorPing = 11002
      MsgInfo( "The destination network was unreachable." )
   CASE xErrorPing = 11003
      MsgInfo( "The destination host was unreachable." )
   CASE xErrorPing = 11004
      MsgInfo( "The destination protocol was unreachable." )
   CASE xErrorPing = 11005
      MsgInfo( "The destination port was unreachable." )
   CASE xErrorPing = 11006
      MsgInfo( "Insufficient IP resources were available." )
   CASE xErrorPing = 11007
      MsgInfo( "A bad IP option was specified." )
   CASE xErrorPing = 11008
      MsgInfo( "A hardware error occurred." )
   CASE xErrorPing = 11009
      MsgInfo( "The packet was too big." )
   CASE xErrorPing = 11010
      MsgInfo( "The request timed out." + hb_osNewLine() + "Internet Connection Not Available" )
   CASE xErrorPing = 11011
      MsgInfo( "A bad request." )
   CASE xErrorPing = 11012
      MsgInfo( "A bad route." )
   CASE xErrorPing = 11013
      MsgInfo( "The time to live (TTL) expired in transit." )
   CASE xErrorPing = 11014
      MsgInfo( "The time to live expired during fragment reassembly." )
   CASE xErrorPing = 11015
      MsgInfo( "A parameter problem." )
   CASE xErrorPing = 11016
      MsgInfo( "Datagrams are arriving too fast to be processed" + hb_osNewLine() + ;
               " and datagrams may have been discarded." )
   CASE xErrorPing = 11017
      MsgInfo( "An IP option was too big." )
   CASE xErrorPing = 11018
      MsgInfo( "A bad destination." )
   CASE xErrorPing = 11050
      MsgInfo( "A general failure" + hb_osNewLine() + ;
               "This error can be returned for some malformed ICMP packets." )
   OTHERWISE
      MsgInfo( "Internet Connection Not Available" )
   ENDCASE

RETURN NIL

// Original code:
// https://groups.google.com/forum/#!topic ... ag2rPxWK_U

#pragma BEGINDUMP

#include <hbapi.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>

int hb_Ping( const char * cp )
{
    HANDLE hIcmpFile;
    unsigned long ipaddr = INADDR_NONE;     // corrected by KDJ
    DWORD dwRetVal;
    char SendData[32] = "Data Buffer";
    LPVOID ReplyBuffer;
    DWORD ReplySize;

    if( isalpha( cp[0] ) )      //host address is a name
    {
       WSADATA wsaData;
       int     iResult;

       iResult = WSAStartup( MAKEWORD(2, 2), &wsaData );

       if( iResult == 0 )
       {
          struct hostent *remoteHost = gethostbyname( cp );

          if( remoteHost != NULL )
             ipaddr = *(unsigned long *) remoteHost->h_addr_list[0];

          WSACleanup();
       }
    }
    else
       ipaddr = inet_addr( cp );

    if (ipaddr == INADDR_NONE)
        return 1;

    hIcmpFile = IcmpCreateFile();
    if (hIcmpFile == INVALID_HANDLE_VALUE)
        return 2;

    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
    ReplyBuffer = (VOID*) malloc(ReplySize);
    if (ReplyBuffer == NULL)
    {
        IcmpCloseHandle(hIcmpFile);
        return 3;
    }


    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
        NULL, ReplyBuffer, ReplySize, 1000);

    free(ReplyBuffer);

    IcmpCloseHandle(hIcmpFile);

    if (dwRetVal == 0)
        return GetLastError();

    return 0;

}

HB_FUNC( HB_PING )
{
   hb_retni( hb_Ping( hb_parc( 1 ) ) );
}

#pragma ENDDUMP

// FIN / END
 


Regards, saludos
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7826
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: HB_PING() under Harbour

Postby Natter » Thu Nov 25, 2021 2:04 pm

Thanks, everything works!
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 83 guests