Manu,
Buena observación la de mirar en errapi.c, no se me había ocurrido
Aqui esta! Mi ejemplo anterior pasado integramente a bajo nivel (lenguaje C)
En un siguiente ejemplo pondré algo más simple, sin usar un objeto en C++.
Code: Select all
// Using a C++ Class and object from a Harbour Class and object
// Build it using samples\buildhm.bat or samples\build64.bat or add -P flag to bcc32 in samples\buildh.bat
#include "FiveWin.ch"
function Main()
local o := Test():New()
MsgInfo( ValType( o:pCppObj ) )
o:One()
MsgInfo( o:lData1 )
o:lData1 = 345
MsgInfo( o:lData1 )
o:End()
MsgInfo( ValType( o:pCppObj ) )
return nil
/*
CLASS Test
DATA pCppObj
METHOD New() INLINE ::pCppObj := TestNew( Self ), Self
METHOD One() INLINE TestOne( ::pCppObj )
METHOD lData1 INLINE TestGetData1( ::pCppObj )
METHOD _lData1( nVal ) INLINE TestSetData1( ::pCppObj, nVal )
METHOD End() INLINE TestEnd( ::pCppObj ), ::pCppObj := nil
ENDCLASS
*/
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include <hbapicls.h>
#include <hbstack.h>
#include <windows.h>
class Test
{
public:
PHB_ITEM pPrgObj;
long lData1;
char * cData2;
BOOL bData3;
void Create( void );
void One( void );
};
void Test::Create( void )
{
lData1 = 123;
}
void Test::One( void )
{
MessageBox( 0, "One", "from C++ level", 0 );
}
HB_FUNC_STATIC( TESTNEW )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
Test * pTest = new Test;
pTest->Create();
pTest->pPrgObj = hb_gcGripGet( pSelf );
hb_arraySetPtr( pSelf, 1, pTest ); // pSelf[ 1 ] = pTest;
hb_itemReturn( pSelf ); // return Self
}
HB_FUNC_STATIC( TESTGETPCPPOBJ )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arrayGet( pSelf, 1, hb_stackReturnItem() );
}
HB_FUNC_STATIC( TESTONE )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
Test * pTest = ( Test * ) hb_arrayGetPtr( pSelf, 1 );
pTest->One();
}
HB_FUNC_STATIC( TESTGETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
Test * pTest = ( Test * ) hb_arrayGetPtr( pSelf, 1 );
hb_retnl( pTest->lData1 );
}
HB_FUNC_STATIC( TESTSETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
Test * pTest = ( Test * ) hb_arrayGetPtr( pSelf, 1 );
pTest->lData1 = hb_parnl( 1 );
}
HB_FUNC_STATIC( TESTEND )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
Test * pTest = ( Test * ) hb_arrayGetPtr( pSelf, 1 );
hb_gcGripDrop( pTest->pPrgObj );
delete pTest;
hb_arrayDel( pSelf, 1 ); // pSelf[ 1 ] = nil
}
HB_FUNC( TEST )
{
static HB_USHORT usClassH = 0;
if( usClassH == 0 )
{
usClassH = hb_clsCreate( 1, "TEST" ); // 1 DATA pCppObj
hb_clsAdd( usClassH, "PCPPOBJ", HB_FUNCNAME( TESTGETPCPPOBJ ) );
hb_clsAdd( usClassH, "LDATA1", HB_FUNCNAME( TESTGETDATA1 ) );
hb_clsAdd( usClassH, "_LDATA1", HB_FUNCNAME( TESTSETDATA1 ) );
hb_clsAdd( usClassH, "ONE", HB_FUNCNAME( TESTONE ) );
hb_clsAdd( usClassH, "NEW", HB_FUNCNAME( TESTNEW ) );
hb_clsAdd( usClassH, "END", HB_FUNCNAME( TESTEND ) );
}
hb_clsAssociate( usClassH ); // Creates an object of Class usClassH
}
#pragma ENDDUMP