Dynamic event handling using scripting languages 
Author Message
 Dynamic event handling using scripting languages

Hi,

I've developed an ActiveX control that supports connection points. I
want to allow scripts to handle events fired by my control. If I create
a named object in the HTML file using <OBJECT ....></OBJECT> it works
fine. However, the events don't work if I dynamically create the
ActiveX control, using CreateObject(...)

Can this be done using VBScript (or Javascript)?

If not, can I write a function in my ActiveX control (say,
AttachObject) that will allow the script to attach to the control by
making a call like

MyControl.AttachObject GetRef("EventHandler")

where EventHandler is the function that will receive events.

TIA

--
Rohitab Batra
http://www.*-*-*.com/

Sent via Deja.com
http://www.*-*-*.com/



Mon, 07 Jul 2003 22:30:16 GMT  
 Dynamic event handling using scripting languages
You can use ScriptX to connect event handlers for objects created on-the-fly via (vbs) CreateObject
or (js) new ActiveXObject().

MeadCo's ScriptX
http://www.meadroid.com/scriptx/

No license is required for this sort use - advanced IE printing features require a license.

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> Hi,

> I've developed an ActiveX control that supports connection points. I
> want to allow scripts to handle events fired by my control. If I create
> a named object in the HTML file using <OBJECT ....></OBJECT> it works
> fine. However, the events don't work if I dynamically create the
> ActiveX control, using CreateObject(...)

> Can this be done using VBScript (or Javascript)?

> If not, can I write a function in my ActiveX control (say,
> AttachObject) that will allow the script to attach to the control by
> making a call like

> MyControl.AttachObject GetRef("EventHandler")

> where EventHandler is the function that will receive events.

> TIA

> --
> Rohitab Batra
> http://www.rohitab.com/

> Sent via Deja.com
> http://www.deja.com/



Tue, 08 Jul 2003 04:15:58 GMT  
 Dynamic event handling using scripting languages
Thanks Michael,

I'll try it out. But what I wanted was, how can I add support like this
to my own Object?

Rohitab
http://www.rohitab.com/

Sent via Deja.com
http://www.deja.com/



Tue, 08 Jul 2003 23:11:18 GMT  
 Dynamic event handling using scripting languages

Quote:
> ...But what I wanted was, how can I add support like this
> to my own Object?

I don't think you can.  At least I don't know of *any* component that automatically connect to event
sinks automatically when created using CreateObject.  That would be a feature of a specific
implementation of a CreateObject method.  Sort of like WScript's CreateObject where it's optional,
not automatic.  In WScript it's also possible to connect after-the-fact by using ConnectObject, but
even that's very dependent on the specific object you're trying to connect to (as explained on the
ConnectObject doc remarks).

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

http://www.deja.com/



Wed, 09 Jul 2003 14:53:40 GMT  
 Dynamic event handling using scripting languages
I tried out ScriptX and it worked fine.

Quote:
> I don't think you can.  At least I don't know of
> *any* component that automatically connect to event
> sinks automatically when created using CreateObject.

I don't want to automatically connect. I want to add another method to
my object, so that I can connect to it from VBScript with a call like

MyObject.AddCallback GetRef("VBScriptEventHandler")

I know it can be done because I've seen objects that do it. Even
ScriptX does something similar. Infact more complex. It connects
*another* object to an event handler.

Quote:
> even that's very dependent on the specific object
> you're trying to connect to (as explained on the
> ConnectObject doc remarks).

I'm willing to add any support that is required. Please have a look at
this:

http://www.mindspring.com/~mark_baker/hostobjects.htm#HOE19

There is some pseudo code that explains how it can be done, but I can't
get it to work.

I tried to implement is using the following code:

STDMETHODIMP CSomeObject::AddCallback(IUnknown *pUnknown)
{
        AFX_MANAGE_STATE(AfxGetStaticModuleState())

        DWORD   dwCookie;
        AtlAdvise(GetUnknown(), pUnknown, DIID__ISomeObjectEvents,
&dwCookie);
        return S_OK;

Quote:
}

The call to AtlAdvise fails when it QueryInteface's pUnknown for
DIID__ISomeObjectEvents.

Rohitab Batra
http://www.rohitab.com/

Sent via Deja.com
http://www.deja.com/



Wed, 09 Jul 2003 22:20:34 GMT  
 Dynamic event handling using scripting languages
Now that you can do - at least I've done the same thing using a VB component that was passed a
GetRef object.  Actual the VB component doesn't know or care is the passed object is a VBScript
GetRef object or a "first class" JScript function object.  As far as I can tell, it just has to be
an object that has a default interface member that is a method.

Internally I just Set the passed GetRef object into a class level Variant or Object variable and
called it later.  I've done this with GetRef'd VBScript procedures that were Functions and Subs,
with and without required arguments.  The VB code that used the saved GetRef object reference just
has to know what the call syntax (sub vs function, args vs no args) is in advance.

I'm not a VC++ MFC/ATL programmer (that's *not* a bad thing ;-) so I can't give you a VC++ example.

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> I tried out ScriptX and it worked fine.

> > I don't think you can.  At least I don't know of
> > *any* component that automatically connect to event
> > sinks automatically when created using CreateObject.

> I don't want to automatically connect. I want to add another method to
> my object, so that I can connect to it from VBScript with a call like

> MyObject.AddCallback GetRef("VBScriptEventHandler")

> I know it can be done because I've seen objects that do it. Even
> ScriptX does something similar. Infact more complex. It connects
> *another* object to an event handler.

> > even that's very dependent on the specific object
> > you're trying to connect to (as explained on the
> > ConnectObject doc remarks).

> I'm willing to add any support that is required. Please have a look at
> this:

> http://www.mindspring.com/~mark_baker/hostobjects.htm#HOE19

> There is some pseudo code that explains how it can be done, but I can't
> get it to work.

> I tried to implement is using the following code:

> STDMETHODIMP CSomeObject::AddCallback(IUnknown *pUnknown)
> {
> AFX_MANAGE_STATE(AfxGetStaticModuleState())

> DWORD dwCookie;
> AtlAdvise(GetUnknown(), pUnknown, DIID__ISomeObjectEvents,
> &dwCookie);
> return S_OK;
> }

> The call to AtlAdvise fails when it QueryInteface's pUnknown for
> DIID__ISomeObjectEvents.

> Rohitab Batra
> http://www.rohitab.com/

> Sent via Deja.com
> http://www.deja.com/



Thu, 10 Jul 2003 04:57:11 GMT  
 Dynamic event handling using scripting languages
Thanks Michael,

I've been able to implement it as you suggested. I add the event
handler from VBScript as follows:

------ VBScript ------
<script language="VBScript">
Dim MyObject
Set MyObject = CreateObject("Temp.TempCtl")
MyObject.AddCallback GetRef("EventHandler")

Sub EventHandler
        MsgBox ("Event Received")
End Sub
</script>
------ VBScript ------

In ATL, I call the event handler like this:

------- ATL -------
STDMETHODIMP CTempCtl::AddCallback(IUnknown *pUnknown)
{
        AFX_MANAGE_STATE(AfxGetStaticModuleState())

        CComQIPtr<IDispatch, &IID_IDispatch> pDispatch(pUnknown);

        DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
        pDispatch->Invoke(
                        0,
                        IID_NULL,
                        LOCALE_USER_DEFAULT,
                        DISPATCH_METHOD,
                        &dispparamsNoArgs, NULL, NULL, NULL);
        return S_OK;

Quote:
}

----- ATL ---------

Rohitab Batra
http://www.rohitab.com/

p.s. I'd still like to know how to implement it using connection points.

Sent via Deja.com
http://www.deja.com/



Mon, 14 Jul 2003 14:47:35 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Handle Events from script using ScriptControl

2. Dynamic event handling?

3. how to handle event of dynamic control array?

4. handling events in dynamic control addition

5. Adding Dynamic Controls and handling the events

6. -~=###--WHAT IS THE DIFFERENCE BETWEEN HANDLED EVENTS AND UN-HANDLED EVENTS?---#=~-

7. Using Event and this to pass object handle to function

8. Event Handling with Microsoft Script Control

9. How to handle activex events in a script

10. event handling in Script Control

11. Could HTML Events be Handled from script file ?

12. event handling in Script Control

 

 
Powered by phpBB® Forum Software