Inhibiting automatic QueryInterface in COM interop.

I

Ilya Konstantinov

Hi,

Suppose[1] in some 3rd-party COM-based API there's a method which
returns an IFooBar object:
HRESULT GetFooBar(IFooBar **pvFooBar);

and then for interop you convert this method to:
IFooBar GetFooBar();

and then, from your .NET code, you'd do:
IFooBar myBar = GetFooBar();
myBar->doSomething(); // at this point, QueryInterface occurs

The problem is, this COM object wasn't written with the best practices
in mind; while the returned pvFooBar is already QI'd and ready for use,
attempting to QI it for IID_IFooBar returns E_NO_SUCH_INTERFACE.
Is there any way in .NET CF to prohibit the automatic QI and just tell
it "Assume the return pointer is already QI'd, and just use it"?

[1] The "imaginary COM object" is actually the Pocket PC 5.0 Imaging
API JPEG encoder (IImageEncoder) whose GetEncodeSink() returns an
IImageSink which cannot QI to IID_IImageSink.
 
I

Ilya Konstantinov

Eventually, I ended up writing an unmanaged wrapper COM object
implementing IFooBar and delegating all its method calls to an inner
IFooBar object *BUT* reimplementing QueryInterface to behave correctly
(as well as proper AddRef and Release). Then I wrote an unmanaged
function, which I DllImport'd, which gets me the misbehaving
implementation of IFooBar inside a wrapper COM object.

P.S. The error I was referring to was E_NOINTERFACE, of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top