描述:
服务器端:(STA)
用ATL实现的一个COM对象OPCGroup,支持连接点(回调接口为IOPCDataCallback)。
该对象有一个函数Read()(客户通过接口,调用此函数),在Read()中,启用了一个线程,来启动真正的读物理设备,该函数的名为AsyncRead(),线程启动后,Read()立即返回。
AsyncRead()执行时间较长。
在读完后,AsyncRead()调用客户端的IOPCDataCallback::OnReadComplete()。
客户端:
Advise()调用成功。
问题是:
1、起初,我直接在AsyncRead()中直接使用m_vec.GetAt(nIndex)取得客户的回调接口指针,然后调用该接口(IOPCDataCallback)的OnReadComplete(),
此时出错,错误类型如下:
// MessageId: RPC_E_WRONG_THREAD
// MessageText:
// The application called an interface that was marshalled for a different thread.
2、然后我考虑到套间的问题,所以做了如下改动:
在主线程中(Read()),使用m_vec.GetAt(nIndex)取得客户的回调接口指针,然后调用CoMarshalInterThreadInterfaceInStream()将该指针列集到一个IStream流对象中,
在线程AsyncRead()中调用CoGetInterfaceAndReleaseStream()从流对象中散集出客户的回调接口指针,
然后调用该接口的OnReadComplete(),此时有出错,错误类型如下:
// MessageId: RPC_E_FAULT(0x80010104L)
// MessageText:
// RPC could not call the server or could not return the results of calling the server.
请高手指点,多谢!