Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Friday, November 01, 2013

Catching a COM+ Event in VBScript

Catching events in Visual Basic 6 was simple---the IDE provided you all the tools to define event handlers in a very easy and intuitive way. Similar approach was carried over to .NET based languages. But what if you are going to use a COM object in VBScript, where you don't have an IDE to help you at all?

Difference between CreateObject and WScript.CreateObject

The first point to note is that you need to use WScript.CreateObject to create your COM object in VBScript, if you want to attach a handler for events raised by the object. WScript.CreateObject takes an additional parameter---a prefix for your VBScript functions which will be called when the event is raised:

Set obj = WScript.CreateObject(ProgID [,strPrefix])

where strPrefix is the starting prefix of the function names which will act as the event handlers for the object created.

Event Handler - Practical Example

Let's say that I have a prog id of "Math.SimpleCalculator" and the event raised by the corresponding object is CalculationComplete. The VBSCript client which can create this object and receive the raised event will be declared as follows:

Set obj = WScript.CreateObject ( "Math.SimpleCalculator", "MyObj_")

Function MyObj_CalculationComplete ()
   MsgBox "Calculation is complete!"
EndFunction

call obj.doSomething()
call WScript.Sleep (1000)

No comments:

Post a Comment