FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Ping con hb_ping() de Daniel con MINGW
Posts: 302
Joined: Fri Apr 23, 2010 04:30 AM
Ping con hb_ping() de Daniel con MINGW
Posted: Sat Apr 28, 2012 05:46 PM
Estoy tratando de usar esta funcion desde MINGW pero no he podido resolver las siguientes funciones:

updftp2.c:(.text+0x1ff): undefined reference to `IcmpCreateFile@0'
updftp2.c:(.text+0x270): undefined reference to `IcmpSendEcho@32'


Code (fw): Select all Collapse
#include "fivewin.ch"

// PRUEBA DE PING //

function main()

   if WSAStartup() != 0
       MsgAlert( "WSAStartup error" )
       return nil
   endif
     
   if hb_Ping( GetHostByName( "www.google.com" ) ) == 0
      Msginfo("Respuesta correcta del host " + AllTrim( GetHostByName( "www.google.com" ) ), "Atenci贸n" )
   else
      Msginfo("Host inaccesible" + AllTrim( GetHostByName( "www.google.com" ) ), "Atenci贸n" )
   endif
  
   Ping( GetHostByName( "www.google.com" ) )
   
   WSACleanUp()

   
return nil


Function Ping(DestinationAddress)
Local IcmpHandle,Replicas,puerto
Local RequestData   :="Probando ping",;
      RequestSize   :=15,;
      RequestOptions:="",;
      ReplyBuffer   :=SPACE(278),;
      ReplySize     :=278,;
      Timeout       := 1000 && Milisegundos de espera

DEFAULT DestinationAddress := "127.0.0.1"

DestinationAddress:=LEFT(ALLTRIM(DestinationAddress)+SPACE(15),15)

MsgGet("Ping...","Ingrese una direcci贸n IP",@DestinationAddress)

IcmpHandle:=IcmpCreateFile()

Replicas  :=IcmpSendEcho(IcmpHandle,;
                          inet_addr(DestinationAddress),;
                          RequestData,;
                          RequestSize,0,;
                          ReplyBuffer,;
                          ReplySize,;
                          Timeout)

IcmpCloseHandle(IcmpHandle)

IF Replicas > 0
   Msginfo("Respuesta correcta del host "+ALLTRIM(DestinationAddress),"Atenci贸n")
ELSE
   Msginfo("Host inaccesible"+ALLTRIM(DestinationAddress)+"Atenci贸n")
ENDIF

Return nil
//

DLL32 FUNCTION WSAGetLastError() AS _INT PASCAL FROM "WSAGetLastError" LIB "wsock32.dll"
DLL32 FUNCTION inet_addr(cIP AS STRING) AS LONG PASCAL FROM "inet_addr" LIB "wsock32.dll"
DLL32 FUNCTION IcmpCreateFile() AS LONG PASCAL FROM "IcmpCreateFile" LIB "icmp.dll"
DLL32 FUNCTION IcmpCloseHandle(IcmpHandle AS LONG) AS LONG PASCAL FROM "IcmpCloseHandle" LIB "icmp.dll"
DLL32 FUNCTION IcmpSendEcho(IcmpHandle AS LONG,;
                            DestinationAddress AS LONG,;
                            RequestData AS STRING,;
                            RequestSize AS LONG,;
                            RequestOptions AS LONG,;
                            ReplyBuffer AS LPSTR,;
                            ReplySize AS LONG,;
                            Timeout AS LONG) AS LONG PASCAL FROM "IcmpSendEcho" LIB "icmp.dll"

#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;
    DWORD dwRetVal;
    char SendData[32] = "Data Buffer";
    LPVOID ReplyBuffer;
    DWORD ReplySize;

    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)
        return 3;
    
    
    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), 
        NULL, ReplyBuffer, ReplySize, 1000);

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

}


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

#pragma ENDDUMP



Slds
Nicanor Martinez M.
Auditoria y Sistemas Ltda.
MicroExpress Ltda.
FW + FWH + XHARBOUR + HARBOUR + PELLES C + XDEVSTUDIO + XEDIT + BCC + VC_X86 + VCC_X64 + MINGW + R&R Reports + FastReport + Tdolphin + ADO + MYSQL + MARIADB + ORACLE
nnicanor@yahoo.com
Posts: 302
Joined: Fri Apr 23, 2010 04:30 AM
Re: Ping con hb_ping() de Daniel con MINGW SOLUCIONADO
Posted: Sun May 06, 2012 06:23 AM
Hola todos,

Me respondo el post, la funcion Hb_ping() del ejemplo de Daniel funciona con MINGW con estas modificaciones:


Code (fw): Select all Collapse
#pragma BEGINDUMP

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

//Agregamos estas declaraciones
extern HANDLE IcmpCreateFile(void);
extern BOOL WINAPI IcmpCloseHandle(HANDLE);
extern DWORD IcmpSendEcho( HANDLE  IcmpHandle,
    IPAddr                 DestinationAddress,
    LPVOID                 RequestData,
    WORD                   RequestSize,
    PIP_OPTION_INFORMATION RequestOptions,
    LPVOID                 ReplyBuffer,
    DWORD                  ReplySize,
    DWORD                  Timeout );

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

    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)
        return 3;
   
   
    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
        NULL, ReplyBuffer, ReplySize, 1000);

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

}


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

#pragma ENDDUMP


Luego creamos la libreria exportando de ICMP.DLL usando impdef.exe de BCC

c:\bcc63\bin\impdef icmp.def icmp.dll

Crea el archivo icmp.def

Code (fw): Select all Collapse
LIBRARY     ICMP.DLL

EXPORTS
    IcmpCloseHandle                @1   ; IcmpCloseHandle
    IcmpCreateFile                 @2   ; IcmpCreateFile
    IcmpParseReplies               @3   ; IcmpParseReplies
    IcmpSendEcho                   @5   ; IcmpSendEcho
    IcmpSendEcho2                  @4   ; IcmpSendEcho2
    do_echo_rep                    @6   ; do_echo_rep
    do_echo_req                    @7   ; do_echo_req
    register_icmp                  @8   ; register_icmp


y por ultimo creamos la lib de MINGW con extension libicmp2.a

c:\mingw\bin\dlltool -d imcp1.def -l libicmp2.a

y la enlazamos y listo,

Slds,
Nicanor Martinez M.
Auditoria y Sistemas Ltda.
MicroExpress Ltda.
FW + FWH + XHARBOUR + HARBOUR + PELLES C + XDEVSTUDIO + XEDIT + BCC + VC_X86 + VCC_X64 + MINGW + R&R Reports + FastReport + Tdolphin + ADO + MYSQL + MARIADB + ORACLE
nnicanor@yahoo.com

Continue the discussion