我有用 C# 编写的 BHO。在主线程的 DocumentComplete 事件处理程序中执行初始化,然后我启动单独的线程并希望在该线程中使用 IMarkupServices 但收到以下错误:
System.InvalidCastException:无法将类型为“mshtml.HTMLDocumentClass”的 COM 对象转换为接口类型“mshtml.IMarkupServices”。此操作失败,因为 IID 为“{3050F4A0-98B5-11CF-BB82-00AA00BDCE0B}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .
以下是初始化的完成方式:
void ieInstance_DocumentComplete(object pDisp, ref object URL)
{
InternetExplorer explorer = pDisp as InternetExplorer;
_ieHtmlDocument2 = (IHTMLDocument2)explorer.Document;
_markupServices = (IMarkupServices)_ieHtmlDocument2;
_markupServices.CreateMarkupPointer(out _markupPointerBeginGlob); // No exception here
_workerThread = new Thread(WorkerThread);
_workerThread.IsBackground = true;
_workerThread.SetApartmentState(Thread.CurrentThread.GetApartmentState());
_workerThread.Start();
}
这是线程过程:
void WorkerThread()
{
_markupServices.CreateMarkupPointer(out _markupPointerBeginGlob); // Exception here!
}
编辑: 在 C++ 中似乎有必要调用以下函数来实现我所需要的:
CoMarshalInterThreadInterfaceInStream
CoGetInterfaceAndReleaseStream
编辑2:
尝试显式调用 CoMarshalInterThreadInterfaceInStream / CoGetInterfaceAndReleaseStream 但仍然没有结果(尝试使用标记服务时抛出相同的异常)