FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour "swaping" functions
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
"swaping" functions
Posted: Sun Sep 07, 2008 10:30 AM
This is a virtual machine "dirty" hack :-) but can result very useful under some circunstances. I place a copy here just in case someone want to test it:

test.prg
#include "FiveWin.ch"

static pOld

function Main()

   pOld := FunSwap( "TIME", "MYTIME" )

   MsgInfo( Time() ) // We have replaced the original Time() function! :-)

return nil

function MyTime()

   local uRet := ExecPtr( pOld ) // in case that we want to call the original function

return "now"

#pragma BEGINDUMP

#include <hbapi.h>

typedef void ( * PFUNC ) ( void );

HB_FUNC( FUNSWAP )
{
   PHB_SYMB symFirst = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 1 ) ) );
   PHB_SYMB symLast  = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 2 ) ) );
   PHB_FUNC pFirst   = symFirst->value.pFunPtr;
   
   symFirst->value.pFunPtr = symLast->value.pFunPtr;
   
   hb_retnl( ( LONG ) pFirst );
}

HB_FUNC( EXECPTR )
{
   PFUNC p = ( PFUNC ) hb_parnl( hb_pcount() );
   
   p();
}   

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
&quot;swaping&quot; functions
Posted: Sun Sep 07, 2008 10:47 AM
Another example:

test.prg
#include "FiveWin.ch"

static pOld

function Main()

   pOld := FunSwap( "DATE", "TOMORROW" )

   MsgInfo( Date() ) // We have replaced the original Date() function! :-)

return nil

function Tomorrow()

   local uRet := ExecPtr( pOld ) // in case that we want to call the original function

return uRet + 1

#pragma BEGINDUMP

#include <hbapi.h>

typedef void ( * PFUNC ) ( void );

HB_FUNC( FUNSWAP )
{
   PHB_SYMB symFirst = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 1 ) ) );
   PHB_SYMB symLast  = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 2 ) ) );
   PHB_FUNC pFirst   = symFirst->value.pFunPtr;
   
   symFirst->value.pFunPtr = symLast->value.pFunPtr;
   
   hb_retnl( ( LONG ) pFirst );
}

HB_FUNC( EXECPTR )
{
   PFUNC p = ( PFUNC ) hb_parnl( hb_pcount() );
   
   p();
}   

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
&quot;swaping&quot; functions
Posted: Sun Sep 07, 2008 11:19 AM
A useful way to create logs or do assertions (James!) :-)

test.prg
#include "FiveWin.ch"

static pOld

function Main()

   pOld := FunSwap( "TEST", "LOGIT" )

   MsgInfo( Test( "Hello", " world!" ) )

return nil

function Test( u1, u2 )

return u1 + u2

function LogIt( u1, u2 )

   local uRet := ExecPtr( u1, u2, pOld ) // in case that we want to call the original function

   MsgInfo( "Test() called with these parameters: " + u1 + ", " + u2 )

return uRet

#pragma BEGINDUMP

#include <hbapi.h>

typedef void ( * PFUNC ) ( void );

HB_FUNC( FUNSWAP )
{
   PHB_SYMB symFirst = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 1 ) ) );
   PHB_SYMB symLast  = hb_dynsymSymbol( hb_dynsymFindName( hb_parc( 2 ) ) );
   PHB_FUNC pFirst   = symFirst->value.pFunPtr;
   
   symFirst->value.pFunPtr = symLast->value.pFunPtr;
   
   hb_retnl( ( LONG ) pFirst );
}

HB_FUNC( EXECPTR )
{
   PFUNC p = ( PFUNC ) hb_parnl( hb_pcount() );
   
   p();
}   

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
&quot;swaping&quot; functions
Posted: Sun Sep 07, 2008 02:41 PM

Antonio,

This looks interesting. Thanks.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
&quot;swaping&quot; functions
Posted: Mon Sep 08, 2008 09:28 AM

For those interested in this issue, we have gone a little further in the spanish equivalent thread, creating a Class TFunction :-)

http://forums.fivetechsoft.com/viewtopic.php?t=12588

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 246
Joined: Sat Mar 03, 2007 08:42 PM
&quot;swaping&quot; functions
Posted: Mon Sep 08, 2008 02:11 PM
Antonio Linares wrote:For those interested in this issue, we have gone a little further in the spanish equivalent thread, creating a Class TFunction :-)

http://forums.fivetechsoft.com/viewtopic.php?t=12588

Interesting concept! :-)

Patrick

Continue the discussion