Welcome to MSDN Blogs Sign in | Join | Help

Script CallBack from COM

Earlier we saw how to call a function on COM class from JScript. But what if one wants to call a function say a callback which is in JScript from COM? Lets see how to achieve this in this post.

 

To make COM and JScript interoperable and allow COM component to call JScript function we need to pass JScript function to the COM component so that COM component receives it as IDispatch. Later to invoke this function, Invoke method on this IDispatch can be used.

 

How? Here is step by step explanation

 

Callback function:

function scriptCallBack()

{

    alert("In script callback");

}

 

Sending callback function to ActiveXObject:

x.SetCallBack(scriptCallBack);

 

COM function in class Test that implements SetCallBack:

STDMETHODIMP CTest::SetCallBack(IDispatch* disp)

{

      m_disp = disp;

      m_disp->AddRef();

      return S_OK;

}

 

COM function that calls CallBack:

STDMETHODIMP CTest::SomeFuncThatCallsCallBack(void)

{

      // Do Something

 

      // Call the callback

      if(m_disp)

      {

            DISPPARAMS dispParams = { NULL, NULL, 0, 0 };

            m_disp->Invoke(

                  DISPID_VALUE,

                  IID_NULL,

                  LOCALE_USER_DEFAULT,

                  DISPATCH_METHOD,

                  &dispParams,

                  NULL,

                  NULL,

                  NULL);

      }

 

      return S_OK;

}

 

Now this works perfectly fine, To test it I write the JScript code:

x.SomeFuncThatCallsCallBack();

 

Attached is the sample code

Published Saturday, August 11, 2007 2:27 AM by Sheetal Kamat
Filed under: , ,

Attachment(s): Sample.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# JScript COM interop...

Here are links to a virtual series written by Sheetal, one of the developers in the JScript team on Jscript

Tuesday, September 25, 2007 12:30 PM by Gaurav Seth's WebLog

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker