APGen Documentation Previous Topic: Script.CreateObject() Next Topic: Script.DisconnectEvents() Parent Topic: Script Object    Script Object
Script.ConnectEvents()
See Also:

The Script.ConnectEvents() method connects script event handlers to an object's events.

Syntax

VBScript:

Script.ConnectEvents object, strHandlerPrefix

JScript:

Script.ConnectEvents( object, strHandlerPrefix );
Object

Script The Script object.

Parameters

object The COM object that fires events.
strHandlerPrefix Script functions named strHandlerPrefix + EventName are called when object fires events.

Notes

APGen connects to the object's default event interface. When the object fires an event, APGen calls a script subroutine named strHandlerPrefix plus the event name.

Script event handling for an object can be disabled by calling Script.DisconnectEvents() and passing in the object.

Script.ConnectEvents()  enables connecting event handlers for objects that are non-createable, or that have already been created.

Script.ConnectEvents() only works for objects that implement the IProvideClassInfo or IProvideClassInfo2 interfaces.  If you receive the run-time error:

Object is not connectable because TypeInfo for the default event interface could not be obtained.

it means the object does not implement IProvideClassInfo(2), and Script.ConnectEvents() can not be used with this object.  In such cases, try using Script.CreateObject() to connect script event handling for the object - Script.CreateObject() is able to connect to more objects than Script.ConnectEvents() can.

When connecting to an in-process COM component (a DLL), script event handlers are normally called while an object method is being executed.  The example below demonstrates this - the Foo events are raised while executing Foo.Bar() or Foo.Bar2().

When connecting to an out-of-process COM component (an EXE), script event handlers are called asynchronously.  For out-of-process components, use Script.Wait() to pause execution and let the event handlers be called.  The Script.Wait() topic gives an example.

Example

This example creates an imaginary "FooBar.Foo" COM object, which fires two events: Event1 and Event2.  These events may occur when Foo.Bar() and Foo.Bar2() are being executed.

<%# @LANGUAGE = JScript #%>
<%#

// Foo Event Handlers
function Foo_Event1(event1Arg)
     {
     // Do something when Foo Event1 occurs
     // ...     
     }

function Foo_Event2(event2Arg)
     {
     // Do something when Foo Event2 occurs
     // ...     
     }

var oFoo = Script.CreateObject("FooBar.Foo", "Foo_");

// Foo.Bar may raise some Foo events
oFoo.Bar();

// Disconnect event handling
Script.DisconnectEvents(oFoo);

// ...

// Reconnect event handling for oFoo
Script.ConnectEvents(oFoo, "Foo_");

// Foo.Bar2 may raise some Foo events
oFoo.Bar2();

// Disconnect event handling again
Script.DisconnectEvents(oFoo);

#%>

Applies To

Script Object