// \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
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 35 guests