不情愿地通过RPC调用QueryInterface
在我的应用程序中,我创建了一个对象 A,该对象创建了一个对象 B,两者都是通过 CreateInstance 实现的。两个对象应该存在于同一个进程中。
现在我看到对象 B 在请求某个接口时返回 E_NOINTERFACE,尽管我在 COM_MAP 中定义了它:
class B:
{
// ....
BEGIN_COM_MAP(B)
COM_INTERFACE_ENTRY(IB)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IXXX) // the interface I'm interested in
END_COM_MAP()
// .....
};
并且 A 代码:
#define FORWARD_ERROR( expr ) { hr=expr; if( !SUCCEEDED( hr ) ) return hr;}
IBPtr b;
FORWARD_ERROR( b.CreateInstance( __uuidof( B ), 0, CLSCTX_INPROC_SERVER ) );
IXXXPtr x;
HRESULT hrIf = b.QueryInterface( __uuidof( IXXX ), x );
// ===> now x is NULL, and hrIf contains E_NOINTERFACE
当我调试它并在 COM_MAP 中放置断点时,我看不到我的源代码在最底层,但是一些ole32.dll的CRpcThread::WorkerLoop
。
我不知道如何指示应该通过 OLE 和 RPC 调用 QueryInterface。有什么想法吗?
In my application, I create an object A, that creates an object B, both via CreateInstance
. Both objects should live in the same process.
Now I see that object B, when asked for a certain interface, is returning E_NOINTERFACE, although I defined it in the COM_MAP:
class B:
{
// ....
BEGIN_COM_MAP(B)
COM_INTERFACE_ENTRY(IB)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IXXX) // the interface I'm interested in
END_COM_MAP()
// .....
};
And the A code:
#define FORWARD_ERROR( expr ) { hr=expr; if( !SUCCEEDED( hr ) ) return hr;}
IBPtr b;
FORWARD_ERROR( b.CreateInstance( __uuidof( B ), 0, CLSCTX_INPROC_SERVER ) );
IXXXPtr x;
HRESULT hrIf = b.QueryInterface( __uuidof( IXXX ), x );
// ===> now x is NULL, and hrIf contains E_NOINTERFACE
When I debug this, and put a breakpoint in the COM_MAP, I don't see my source code in the lowest frame, but some ole32.dll's CRpcThread::WorkerLoop
.
I have no clue how I indicated that the QueryInterface
should be called via OLE and RPC. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的描述,这绝对是编组启动。编组是通过 RPC 进行隧道调用来完成的,因此看起来相当奇怪,但这就是 Windows 上的完成方式。
消费者线程可能使用
COINIT_APARTMENTTHREADED
调用CoInitializeEx()
。由于要创建的对象被标记为Free
,因此它不可能在调用者的公寓中创建(请参阅这个关于公寓的很好的解释)。相反,COM 尝试打开编组,而您可能没有任何东西以便于编组,在这种情况下,CoCreateInstance()
返回E_NOINTERFACE
,因为 COM 内部工作请求 它将用于的一堆接口编组,一旦所有这些请求失败,它就会以E_NOINTERFACE
结束并返回它,这对您来说当然一点也不方便。然后,您从
Free
更改为Both
,这意味着 “Apartment
ofFree
as COM sees fit” COM 被正式允许将对象放入与呼叫者同一公寓,但没有编组是必需的,因此您不会再看到那些看起来很奇怪的错误代码。From you description it was definitely marshalling kicking in. Marshalling is done by tunnelling calls through RPC so it looks rather weird, but that's how it is done on Windows.
The consumer thread likely called
CoInitializeEx()
withCOINIT_APARTMENTTHREADED
. Since the object to create was marked asFree
it couldn't have been created in the caller's apartment (see this very good explanation on apartments). Instead COM tried to turn marshalling on and you likely haven't anything to facilitate marshalling and in such casesCoCreateInstance()
returnE_NOINTERFACE
because COM inner workings request a bunch of interfaces it would use for marshalling and once all those requests fail it ends up withE_NOINTERFACE
and returns it which is of course not convenient for you at all.Then you changed from
Free
toBoth
which means "Apartment
ofFree
as COM sees fit" COM is officially allowed to put the object into the same apartment as the caller and no marshalling is required and so you don't see that weird looking error code anymore.当我从多线程上下文创建对象时,
B
类的线程模型是“自由”的。将B
切换为“两者”解决了问题。The thread model for the
B
class was 'Free', while I was creating the object from a MultiThreaded context. SwitchingB
to 'Both' solved the problem.