[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How do I use CoMarshalInterThreadInterfaceInStream to wrap an IHTMLElement?

Posted on 2009-04-01
7
Medium Priority
?
1,860 Views
Last Modified: 2013-12-08
I need to access an IHTMLElement from another thread, in my Browser Helper Object.  Unfortunately, I've been unable to get it working without causing a segmentation fault when put_innerHTML is called.

Does anyone know how to (correctly) use CoMarshalInterThreadInterfaceInStream in a BHO?
// *********** This is the calls that are made in the main thread ************
       LPSTREAM st;
       CoMarshalInterThreadInterfaceInStream(IID_IHTMLElement, Status, &st);
       m_downloadParam.statusElement = reinterpret_cast<IHTMLElement*>(st);
 
       // Create a thread to download it
       m_pDownloadThread = CreateThread(NULL,
                                                                               0,
                                                                               StartYAIPPDownload,
                                                                               &m_downloadParam,
                                                                               CREATE_SUSPENDED,
                                                                               NULL);
 
// *********** This is the calls that are made in the second thread for downloading ************
DWORD WINAPI StartYAIPPDownload(PVOID pParam)
{
       CBhoApp::DOWNLOADPARAM* parameters =
reinterpret_cast<CBhoApp::DOWNLOADPARAM*>(pParam);
       IHTMLElement* Status = parameters->statusElement;
       BSTR URLToDownload = parameters->strURL;
       YAIPPDownloader* yaippdownloader = new YAIPPDownloader(Status);
       yaippdownloader->Status = Status; // pass the HTML element
       yaippdownloader->AddRef();
       HRESULT hr = URLDownloadToFile(NULL,"http://www.roket-games.com/games/19/gdownload"/*URLToDownload*/,"C:\\test.htm",0,yaippdownloader);
 
       if (hr)
       {
               YAIPPInProgress=false;
               YAIPPStatusText=L"Could Not Download File";
       }
       else
       {
               YAIPPInProgress=false;
               YAIPPStatusText=L"Downloaded File";
       }
       return S_OK;
}
 
// *********** This is the calls that are made in the downloader class's OnProgress function (note that the two Status variables ARE different) ************
if (Status != NULL)
	{
		char ulProgressBuffer [33];
		char ulProgressPercBuffer [33];
		counter++;
		if (ulProgressMax!=0)
			itoa((100/ulProgressMax*ulProgress),ulProgressPercBuffer,10);
		else
			itoa(counter,ulProgressPercBuffer,10);
		itoa(ulProgress,ulProgressBuffer,10);
		std::wstring text;
		text = L"Downloading... ";
		if (ulProgressMax!=0)
			text += ConvertToWideChar(ulProgressBuffer);
		else
			text += ConvertToWideChar(ulProgressPercBuffer);
		BSTR StatText;
		StatText = W2BSTR((text).c_str());
 
		YAIPPStatusText=StatText;
	
		// ************* SEGMENTATION FAULT ON NEXT LINE ****************
		Status->put_innerHTML(YAIPPStatusText);
	}

Open in new window

0
Comment
Question by:hach-que
  • 5
7 Comments
 
LVL 7

Accepted Solution

by:
HalfAsleep earned 2000 total points
ID: 24038929
Where do you do the
CoInitializeEx(NULL, COINIT_MULTITHREADED);

and the

CoGetInterfaceAndReleaseStream  ?

Without these in the other thread, the other thread still has not "received" the marshalled stream.

In one thread you need to to the

CoMarshalInterThreadInterfaceInStream ( longest method name ever)

In the other thread,  you need to use the
CoInitialize and the CoGetInterfaceAndReleaseStream, or the other one that does not release the stream, if you are doing it more than once.


thread 1:
 
_IHTMLElement* pIC;
HRESULT hr = QueryInterface(IID__IHTMLElement, (LPVOID*)&pIC);
 
hRunnerThread = CreateThread(NULL, 0, Runner, pIS, 0, &dwThreadId);
 
IStream* pIS;
hr = CoMarshalInterThreadInterfaceInStream(IID__IHTMLElement, pIC, &pIS);
 
Thread 2 (runner):
CoInitializeEx(NULL, COINIT_MULTITHREADED);
DWORD RetValue = S_OK;
_IHTMLElement* pIC;
HRESULT hr = CoGetInterfaceAndReleaseStream((IStream*) lParam, IID__IHTMLElement, (LPVOID*)&pIC);
_ASSERT(SUCCEEDED(hr));
 
// now you have a valid pointer to interface, the pIC.
pIC->put_innerHTML(YAIPPStatusText);

Open in new window

0
 
LVL 7

Assisted Solution

by:HalfAsleep
HalfAsleep earned 2000 total points
ID: 24038953
In my code, the CoMarshal should be above the CreateThread..sorry about that.
0
 
LVL 7

Assisted Solution

by:HalfAsleep
HalfAsleep earned 2000 total points
ID: 24039001
Also, remember to call pIC->Release() and
CoUninitialize(); before you exit your second thread, where you did the CoInitialize
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 86

Expert Comment

by:jkr
ID: 24041194
Take a look at http://svn.openqa.org/fisheye/browse/~raw,r=57/floyd/trunk/src/cpp/ie/IEBrowserObject.cpp - they do that like
void IEBrowserObject::click(IEHTMLElement *element, int button)
{
    COMAutoPtr<IHTMLDOMNode> comObj = element->getCOMObject();
 
	if (comObj != NULL)
	{
        COMAutoPtr<IHTMLElement2> elementObj = comObj.castTo<IHTMLElement2>(IID_IHTMLElement2);
 
	    if (elementObj != NULL)
	    {
            IStream * stream     = NULL;
            IUnknown *unknownPtr = NULL;
 
            elementObj->QueryInterface(IID_IUnknown, (void **)&unknownPtr);
            if (unknownPtr != NULL)
            {
                CoMarshalInterThreadInterfaceInStream(__uuidof(IHTMLElement2), unknownPtr, &stream);
                unknownPtr->Release();
 
                DWORD result = 0;
 
                sendMessageToThread(MSG_CLICK, (WPARAM)button, (LPARAM)stream, &result);
            }
            else
            {
                LOG4CXX_WARN(_logger, L"Cannot retrieve IDispatch pointer for the element object");
            }
        }
        else
        {
            throw IEBrowserException(L"Could not access the IHTMLElement2 interface for the element");
        }
	}
    else
    {
        throw IEBrowserException(L"Could not access the COM object of the element");
    }
}

Open in new window

0
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24042555
Although you can use a windows handle and the windows message queue to communicate with the other thread, it is a slight cheat.

Don't get me wrong, it is a well known shortcut, and it is even documented on microsoft's own msdn webpages, but it is still not the "correct" way of doing it.

I had first hand experience of this, when I inherited the job of maintaining an old ActiveX automation server.  This old control relied indeed on a windows handle to communicate to the main thread from all its worker threads.  This made the code fairly straightforward, but it had some drawbacks.

First, the component needed to visually be added to a form (it needs a windows handle, remember?).

Second, and this is the reason we had to do it with "proper" marshaling, some environments will not let you cheat with a windows handle like that.  When we wanted to get our old control to work with WinXP/.NET, we discovered that .NET refused to give us such a windows handle (unmanaged code etc? I'm not sure).  We found that the only way to get around this, was to properly implement marshaling.  It was a bit tricky to get right at first, and the other method/cheat sure is easier to find on the net.  But once we had the proper marshaling code into place, we were not dependent on the windows handle and the windows message queue/pump any more.  The control now works in any environment we throw at it, be it .NET, delphi etc, and it does not even need to be tied to a form, it can just be referenced/created in code.
0
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 24042588
I used to have a good link to this stuff at work.  I will see if I can find it tomorrow.
0
 
LVL 2

Author Closing Comment

by:hach-que
ID: 31565215
Thank you so much!  This stumped me for ages.

For anyone reading this answer, make sure you replace _IHTMLElement and IID__IHTMLElement with IHTMLElement and IID_IHTMLElement respectively.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question