C# - Marshal.QueryInterface() - How To Use?

43 Views Asked by At

I'm playing around with C# and COM, and I'm doing pretty well at understanding how to convert C++ COM into C# declarations and code for usage.

I have, however, become stuck on Marshal.QueryInterface().

I know I can directly 'cast' from one IUnknown-derived type to a higher one, which implicitly does the QueryInterface() - which is neat. But for my learning, could someone tell me how I use Marshal.QueryInterface(), please? I'm struggling with interfaces versus IntPtrs.

The declaration is

Marshal.QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv) 
  • pUnk needs needs to come from an existing defined interface; the compiler seems happy with Marshal.GetIUnknownForObject(my_ISomething) (am I heading for a world of pain here?).

  • The GUID is easy - I have an example from elsewhere.

  • ppv I declare as IntPtr... but how do I get the ISomething2 interface back out of it?

There's this article here, but the answer was 'don't'. I'm trying to learn here - I will be using the simple 'cast' in the real thing.

Thanks.

0

There are 0 best solutions below