Click to See Complete Forum and Search --> : Problem with marshaling of a com interface


robin83
February 24th, 2006, 07:03 PM
Hi,
I have an addin which automates an instace of a cad program (Solid Edge). The interface to the application is known in my addin. Now I wanted to create a worker thread to do something with Solid Edge. I used a Stream to get the interface into the thread. Problem occurs with the first call to the interface. The program doesn't crash but the thread hangs. UI runs just fine. But the method of the interface doesn't return. Any ideas?

The following is my (simplified) code:
CHelper::CHelper(ApplicationPtr pAppPtr)
{
CoInitialize(NULL);

HRESULT res = CoMarshalInterThreadInterfaceInStream(__uuidof(ApplicationPtr),
(LPUNKNOWN)pAppPtr, &m_pStream);
}

CHelper::~CHelper(){ CoUninitialize (); }

UINT threadCreate (LPVOID pParam)
{
CoInitialize(NULL);

CHelper* pHelper = (CHelper*)pParam;
ApplicationPtr pAppPtr;
HRESULT hr = CoGetInterfaceAndReleaseStream(pHelper->GetStream(),
__uuidof(ApplicationPtr), (void**) &pAppPtr);

if (SUCCEEDED(hr))
{
//The thread hangs on the following line
DocumentsPtr pDocs = pAppPtr->GetDocuments();
//...
}

CoUninitialize()
return 0;
}

void CHelper::DoCreate(int &nUniqueID, CString &strCreatedPart)
{
//start the Thread:
CWinThread *pthreadCreate = AfxBeginThread (threadCreate, this);
//... wait for the thread and do some other stuff
}

Best regards,
Robin.

MikeAThon
February 24th, 2006, 09:58 PM
I'm on very shaky ground here. But many parts of COM require a message loop so as to serialize asynchronous COM communication. Your worker obviously lacks such a message loop.

Does it work in the UI thread and not in the worker? If so, it might be a message loop issue.

Sorry if this is no help or if I am completely off-base.

Mike

robin83
February 25th, 2006, 04:35 AM
Does it work in the UI thread and not in the worker? If so, it might be a message loop issue.

I put the call to CoGetInterfaceAndReleaseStream in the ui thread and voila all calls to the interface work nicely. So abviously my grounds are even more shaky than yours. How to solve the message loop issue? Any ideas? Thanks.

Robin.

robin83
February 25th, 2006, 05:48 AM
Thanks Mike! That was the hint I was seeking for. I implemented something like that in the UI Thread and it works fine!

// Start the message loop.

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}