xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_Exec()

Executes a function, procedure or method from its pointer.

Syntax

HB_Exec( pFunction_or_Method_Pointer [, <NIL_or_SelfObject> [, ...] ] )

or

HB_Exec( pSomeFunctionPinter [, NIL, Arg1 [, ArgN] ] )

or

HB_Exec( pSomeFunctionPinter [, Self, Arg1 [, ArgN] ] )

Arguments

<pFunctionPointer>
This is a pointer to a function or procedure to execute. It can be obtained with the function-reference operator or the HB_FuncPtr() function. If such a pointer is used, the second parameter must be NIL.
<pMethodPointer>
This is a pointer to an object's method to execute. It can be obtained with the HB_ObjMsgPtr() function. If such a pointer is used, the second parameter must be an object.
<oObject>
This is an object whose method is executed.
<nArg1 [, ArgN]>
An optional list of parameters can be specified. They are passed to the function, method or procedure when executed.

Return

HB_Exec() returns the result of the executed function, method or procedure.

Description

Function HB_Exec() accepts as first parameter a pointer to a function, procedure or method. If a function/procedure pointer is used, the second parameter must be NIL. In case of a method pointer, the second parameter must be an object. All other optional parameters are passed on to the executed function, procedure or method.

A pointer is a special data type in xHarbour and is of Valtype()=="P". It represents the memory address of a function, procedure or method. If a memory address is known, the corresponding routine can be executed without the need of looking up the symbolic name. This can lead to performance advantages.

Info

See also:@(), Eval(), HB_ExecFromArray(), HB_FuncPtr(), HB_ObjMsgPtr()
Category: Indirect execution , Pointer functions , xHarbour extensions
Source:vm\eval.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example uses a function pointer of a built-in function and
// a method pointer to a user-defined class for HB_Exec().

   #include "hbclass.ch"

   PROCEDURE Main
      LOCAL oObject
      LOCAL pPointer := ( @QOut() )

      // displays "Hello World"
      HB_Exec( pPointer, NIL, "Hello", "World" )

      oObject := Test():new( "xHarbour" )

      pPointer := HB_ObjMsgPtr( oObject, "display" )

      // displays five times "xHarbour"
      HB_Exec( pPointer, oObject, 5 )
   RETURN


   CLASS Test
     PROTECTED:
     DATA name

     EXPORTED:
     METHOD init CONSTRUCTOR
     METHOD display
   ENDCLASS

   METHOD init( cName ) CLASS Test
      ::name := cName
   RETURN self

   METHOD display( nTimes ) CLASS Test
      LOCAL i
      IF nTimes == NIL
         nTimes := 1
      ENDIF

      FOR i:=1 TO nTimes
         QOut( ::name )
      NEXT
   RETURN self

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe