Share via


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

Sample.zip

Comments

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