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)
pUnkneeds needs to come from an existing defined interface; the compiler seems happy withMarshal.GetIUnknownForObject(my_ISomething)(am I heading for a world of pain here?).The GUID is easy - I have an example from elsewhere.
ppvI declare asIntPtr... but how do I get theISomething2interface 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.