Share this page 

Override system functionsTag(s): Powerscript


(thanks to J.Lakeman for this HowTo)

Let's say you want to override the behaviour of a system function, but you still want the option of calling it. For example you might have a client application that can also run as a service, so you might want calls to messagebox to be logged to a file instead of displayed.

In PB5 you could create your own instance of systemfunctions and call that. But since PB6 this is no longer possible. Powerbuilder decides which method you are calling at compile time.

For example, if you wrote a new messagebox method it won't be called by existing objects until your application is rebuilt. So what we need is an object that has compiled in calls to the real system functions, that won't get recompiled when we rebuild the application.

First up, create a new target application. Then build an nvo with methods that call the system functions. First up, create a new target application. Then build an nvo with methods that call the system functions. eg:

forward
global type n_cst_systemfuncs from nonvisualobject
end type
end forward
 
global type n_cst_systemfuncs from nonvisualobject autoinstantiate
end type
 
forward prototypes
public function integer of_messagebox(string c1, string t2)
end prototypes
 
public function integer of_messagebox(string c1, string t2);
  RETURN messagebox(c1, t2)
end function
 
on n_cst_systemfuncs.create
call super::create
TriggerEvent( this, "constructor" )
end on
 
on n_cst_systemfuncs.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

You can generate the syntax for all the built in global functions from the class definition of the real systemfunctions object. Excluding any methods with a variable number of arguments.

Build this nvo into a PBD. And add it to the library list of your application.

Now you've got a pointer to the standard system function that you can call even when you have overriden it.

global type messagebox from function_object
end type
 
forward prototypes
global function integer messagebox (string as_caption, string as_text)
end prototypes
 
global function integer messagebox &
  (string as_caption, string as_text);
n_cst_systemfuncs ln_funcs
IF gb_service THEN
   f_log(as_text)
   RETURN 1
ELSE
   RETURN ln_funcs.of_messagebox(as_caption, as_text)
END IF
end function