"swaping" functions

"swaping" functions

Postby Antonio Linares » 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
Code: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sun Sep 07, 2008 10:47 am

Another example:

test.prg
Code: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sun Sep 07, 2008 11:19 am

A useful way to create logs or do assertions (James!) :-)

test.prg
Code: Select all  Expand view
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby James Bott » Sun Sep 07, 2008 2:41 pm

Antonio,

This looks interesting. Thanks.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Antonio Linares » Mon Sep 08, 2008 9: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Patrick Mast » Mon Sep 08, 2008 2: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
User avatar
Patrick Mast
 
Posts: 246
Joined: Sat Mar 03, 2007 8:42 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 84 guests