I am trying to execute this function:
public static int QueryInterface(
IntPtr pUnk,
ref Guid iid,
out IntPtr ppv
)
where
pUnk
Type: System.IntPtr
The interface to be queried.
Basically, Marshal.QueryInterface requests a pointer to a specified interface from a COM object. There is a number of interfaces I would like to query (all from IPersist), so how do I go about obtaining a reference pointer to these interfaces?
Note: IPersistStorage is one of them.
edit (this works):
// Use the CLSID to instantiate the COM object using interop.
Type type = Type.GetTypeFromCLSID(myGuid);
Object comObj = Activator.CreateInstance(type);
// Return a pointer to the objects IUnknown interface.
IntPtr pIUnk = Marshal.GetIUnknownForObject(comObj);
IntPtr pInterface;
Int32 result = Marshal.QueryInterface(pIUnk, ref myGuid, out pInterface);
Read the last line of the remarks section on the
Marshal.QueryInterface()page.I believe you’re looking for the
Marshal.GetComInterfaceForObject()method.