Link to home
Start Free TrialLog in
Avatar of postrowski
postrowski

asked on

How do I talk to HTML from a worker thread in an ATL ActiveX control?

I want to create an ActiveX control that spawns a worker thread to handle Socket communication stuff. When it recieves socket messages, it should be able to call a Javascript function in HTML. I'm having problems when I call:

   HRESULT hr = m_spClientSite->QueryInterface(IID_IServiceProvider, (void **)&pISP);

The resultant hr is always 0x8000FFFF. As such, I can't drill down to find the IHTMLWindow2 interface, upon which I'd like to call execScript(...).
If I make this function call from the UI thread, everything is OK, but thats not what I need. I understand that I can pass a message from the worker thread to the UI and have the UI thread make the call for me also, but again, thats not what I need.

I've also tried getting the IHTMLWindow2 interface pointer in the UI thread, then calling CoMarshalInterface on that pointer, then calling CoUnmarshalInterface on that object from the worker thread, but that call fails with a value of 0x8003001e. I'm pretty sure this is the right way to go, I think I'm just doing it wrong.

So what I'm looking for are the steps to create a bare-bones ATL project that creates a worker thread, and that worker thread makes a call to execScript on the IHTMLWindow2 interface from m_spClientSite.

If anyone wants, I'm more than happy to email them the bare-bones project files that I've created so far.
Avatar of tyronen
tyronen

Try this:

Have your control implement either the IOleObject or the IObjectWithSite interfaces.  You can just inherit these by using the ATL template IOleObjectImpl or IObjectWithSiteImpl.  The first is also available using the ATL Object Wizard in Visual C++.

Then do this:

IOleContainer *spContainer;
m_spClientSite->GetContainer(&spContainer);
IHTMLDocument2 *spDoc;
HRESULT hr = spContainer->QueryInterface(IID_IHTMLDocument2,(void **)&spDoc);
if (FAILED(hr))
return E_NOINTERFACE;
IHTMLwindow2 *spWin;
hr = spDoc->get_parentWindow(&spWin);

Hope this helps

tyronen


Avatar of postrowski

ASKER

I tried this, but it doesn't work. The call to m_spClientSite->GetContainer(&spContainer); doesn't modify spContainer.
In order for the CoUnmarshalInterface function to work, your worker thread must be running in a different apartment.  I don't think client-side controls support multiple apartments.

Have you tried simply passing the interface pointer from one thread to the other?

tyronen
yeah, I've passed the interface pointer, but when I call QueryInterface on it, it always returns 0x8000FFFF.
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had an answer for you.

The link to the Community Support area is: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial