Studio:Callback functions in Perl scripts

From STRIDE Wiki
Jump to: navigation, search

To use callback functions in Perl scripts, use LetProperty(). This is an ActiveState Perl requirement for assigning a COM object (e.g., another interface) to a read/write property.

The following example demonstrates a user callback registration service with an owner callback function:

# The following example illustrates how to script a function pointer
# that has been qualified with the scl_ptr_flist pragma. 
# Perspective: User of the regCallBack interface, Owner of the callBack interface
# Standard Perl setup
use strict;
use Carp;
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
# Create a reference (shortcut) to the User instance of the regCallBack function
my $uRegCallBack = $main::ascript->Functions->Item("regCallBack")->User;
# Create a reference (shortcut) to the Owner instance of the regCallBack function
my $oCallBack = $main::ascript->Functions->Item("callBack")->Owner;
# Register for ownership of the callback function
$oCallBack->Register();
# Assign the callback function to the registration function's parameter cb
$uRegCallBack->ParameterList->LetProperty("cb", $oCallBack);
# Call the registration function passing in the callback function
$uRegCallBack->Call();
# Wait for the callback function to be called
my $event = $main::ascript->WaitForEvent();
# If the received event was the callback function
if ($event->Name == $oCallBack->Name) {
   # Process the function call
   # return the callback function to the user
   $oCallBack->Return();
}