当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Is it possible to determine the underlying COM component type from an interface pointer in .NET interop?

问题描述:

I'm using a COM library in .NET. The vendor has provided primary interop assemblies with documentation. However, a particular method returns an interface pointer but the documentation does not tell me what the underlying component type is (so I have no idea what other interfaces I can QI to).

Is it possible to determine what the underlying component returned from this method is if all I have is an interface pointer?

网友答案:

In general, simple answer is no. COM always hides underlying objects, it does not have refection like C#.

If you own the code that has created it, of course you can keep track, or the object might tell you. Also, you can try some hacks, like checking the loaded dlls in your process, querying all interface, but this all depends on your specific problem.

网友答案:

No, there's no way to find out other than by trying. Where a useful start of actually trying needs to be the documentation. This is a core design feature of COM, the implementation is carefully hidden behind the interfaces.

Not really different in .NET, other than having the Reflection workaround which you shouldn't use either.

网友答案:

This MSDN Magazine article (VB source code included) may be useful: Inspecting COM Objects With Reflection.

网友答案:

If you can guess what it might be you can ask it if it can cast itself to that and see what it replies. See the IUnknown interface's QueryInterface method. In fact it doesn't cast itself, it might return some implementation from an inner object of it, or from some other helper object it spawns on demand etc.

Also can see if it implements ITypeInfo or IDispatch and make use of those. See the link groverboy already mentioned: Inspecting COM Objects With Reflection

分享给朋友:
您可能感兴趣的文章:
随机阅读: