从新线程调用时出现 COM 0x8001010E 错误

COM 0x8001010E error when invoking from a new thread

我有一个 c++ DLL,它使用 IDispatch 接口调用第三方 DLL 上的方法。如果我从原始线程调用这些方法,但如果启动一个新线程(不是调用 CoCreateInstance 的线程)以在它抛出 0x800101E 错误后调用该方法,它就会起作用。我还在第二个线程上尝试了 CoInitialize,但没有成功。

错误 0x8001010E 是 RPC_E_WRONG_THREAD "The application called an interface that was marshalled for a different thread."。

您违反了 COM 单元规则,并且试图在不属于该指针有效单元的线程上使用接口指针。要将接口指针传递给另一个公寓,请使用封送处理。

参见What is COM marshaling and how do I use it? and CoMarshalInterThreadInterfaceInStream function, and this Understanding The COM Single-Threaded Apartment