We help IT Professionals succeed at work.

Creating or returning objects in IDispatchEx to browser JScript engine

JimU
JimU asked
on
1,624 Views
Last Modified: 2010-05-18
I have an ATL component that is being used in IE via JScript.   I need to return an object from this C++ ATL component so that it can be used in the JScript engine.  For example:

Pseudo-JScript-code:
<object name="MyObject" CLSID="03492034-MY-CLASS-ID">
<script language="JScript">
function Foo()
{
   result = MyObject.CallIntoATL();
   //result["SomeAttribute"] will contain something set in the ATL component
}

Pseudo-C++-code for ATL component:
MyObject::InvokeEx(DISPID id, LCID lcid, WORD wFlags, DISPPARAMS* pdp, VARIANT* pvarRes, EXCEPINFO* pei, IServiceProvider* pspCaller)
{
    // Do some stuff, check the id or whatever to call the method

    pvarRes->vt = VT_DISPATCH;  // Correct??
    pvarRes->pdispVal = ????;  // Need object with IDispatchEx interface?

   // Basically I need to return a new JScript object in pvarRes.
}

Now looking at the Microsoft site I see an example that is probably what I want to do (look at the IDispatchEx stuff near the middle):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/37a3303f-f78e-4b5b-aac8-b836c92819de.asp

What they do is use "IDispatchEx *pdexScript" to create an object in the script engine and then I can return that in pvarRes.  My problem is how do I get the IDispatchEx interface of the script engine?  Their example just starts like:

BOOL test(IDispatchEx *pdexScript)
{

OK fine, but where does pdexScript come from?

I assume I can use the pspCaller service provider on my ATL object's InvokeEx method to query and get the IDispatchEx interface of the script engine but I can't figure how to do it.

Thanks for any help.
Comment
Watch Question

Ralf KlattEmployee in Civil Service
CERTIFIED EXPERT

Commented:
Hi,

Maybe this article/sample at http://www.codeproject.com/com/dispexsinkconnector.asp can help you to better understand IDispatchEx interface usage!


Best regards,
Raisor

Author

Commented:
Yes, I have seen that.  I probably should have asked my question differently.  I understand how IDispatchEx and InvokeEx work.

What I don't understand is how to get the IDispatchEx interface of the scripting engine from within the ATL component.  I think I need to use the pspCaller IServiceProvider to query up the chain and get the IDispatchEx interface of the current script but I have no idea how to do it.   pspCaller->QueryInterface(IID_IDispatchEx, ...) fails with NOINTERFACE.

Author

Commented:
I have found that I can do this:

      IActiveScriptSite* iface;
      hr = pspCaller->QueryService(SID_GetScriptSite, IID_IActiveScriptSite, (void**)&iface);

But IActiveScriptSite doesn't do me much good.  If I could get the IActiveScript interface then I could use its GetScriptDispatch to get the interface I need.

The following does not work:
hr = pspCaller->QueryService(SID_GetScriptSite, IID_IActiveScript, (void**)&iface);

The only other services I know of are SID_GetCaller (I have no idea what this is and can't find any documentation whatsoever) and SID_SInternetSecurityManager.

Author

Commented:
No idea what SID_GetCaller is.

      hr = pspCaller->QueryService(SID_GetCaller, IID_IUnknown, (void**)&iface);
      hr = pspCaller->QueryService(SID_GetCaller, IID_IDispatch, (void**)&iface);
      hr = pspCaller->QueryService(SID_GetCaller, IID_IDispatchEx, (void**)&iface);
      hr = pspCaller->QueryService(SID_GetCaller, IID_IActiveScript, (void**)&iface);
      hr = pspCaller->QueryService(SID_GetCaller, IID_IActiveScriptSite, (void**)&iface);
      hr = pspCaller->QueryService(SID_GetCaller, IID_IInternetSecurityManager, (void**)&iface);

They all return S_OK but the iface is NULL so it's not working.

As you can see I'm just flailing around in the dark here.  Documentation is difficult or impossible to find.

If I can get the IDispatchEx interface of the script engine then I can look up the "Object" method and create a new object to be returned...
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Ralf KlattEmployee in Civil Service
CERTIFIED EXPERT

Commented:
Hi,

I'm glad that you've found a solution to your problem ... and ... --->>>    it's great that you share the solution with the EE Community ...

... thank you JimU!


Best regards,
Raisor

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.