Scripting Dynamic Callbacks

Is this possible?

Wednesday, March 23, 2005 by Dangerousdaze | Discussion: DesktopX

I'm playing around with scripting objects. I have a COM control "myControl" that has a method "subscribe" to create an asynchronous subscriber of messages:



myControl.subscribe "params"



This control actually creates a callback called _newMessage which it fires whenever a new message arrives, so in my case I need to code something like this:



sub myControl_newMessage

[Code to handle the new message]
end sub



I've created this in my script but it doesn't fire. Can you create event handlers on the fly for events from COM objects?



Thanks!



Nick
Dangerousdaze
Reply #1 Sunday, March 27, 2005 6:24 PM
Anyone have any ideas on this? I know the COM component is working because I can publish messages from DX3 and see them in other applications but whatever I try I just can't get the myControl_newMessage callback to fire. If I can get this to work then I can upgrade to DX Pro and start cranking out Gadgets...

Nick
thomassen
Reply #2 Monday, March 28, 2005 7:10 AM
Have a look into the WithEvents statement. Not sure if it helps, but it was all I could think of right now.
Dangerousdaze
Reply #3 Monday, March 28, 2005 10:14 AM
Unfortunately WithEvents is not supported in VBScript. I'm wondering if JScript will help. Either that or I might have to write a plugin which seems like an awful lot of trouble to go to. It just seems a shame - Stardock have given us the ability to embed ActiveX controls but that support is very limited if we cannot consume their events.

Nick
JavaScout
Reply #4 Tuesday, March 29, 2005 12:42 AM
How are you registering the callback ?
Here is an example of a callback function that works.
The interesting peice is setting the call back function
http.onreadystatechange=GetRef("processMsg")

Hope this helps

Dim http
'Called when the script is executed
Sub Object_OnScriptEnter
getNewMessage()
MsgBox("done on enter")

End Sub
'Called when the script is terminated
Sub Object_OnScriptExit
On Error Resume Next
http.abort
Set http = Null
End Sub

Function getNewMessage()
Set http = createObject("microsoft.xmlhttp")
http.onreadystatechange=GetRef("processMsg")
http.Open "GET","http://www.w3schools.com/xml/note.xml",True
http.Send
End Function

Function processMsg()
If http.readyState=2 Then
MsgBox("http request started")
End If
If http.readyState=4 Then
MsgBox("http response complete")
End If
End Function
Dangerousdaze
Reply #5 Saturday, April 2, 2005 8:44 AM
Thanks for the reply. Unfortunately the component in question does not supply a property equivalent to onreadystatechange. The documentation just states:

# Declare a variable for the listener.
Dim WithEvents myListener as TibrvListener

This declaration triggers VB to automatically define a private subroutine callback stub named myListener_OnMsg. The program must complete this method stub for each listener object.

Nick

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!



web-wc01