How to get a pointer to an interface?

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 the specified interface from a COM object. There are a number of interfaces that I would like to request (all from IPersist), so how do I need to get a link 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); 
+4
source share
1 answer

Read the last line of the notes section on the Marshal.QueryInterface() page.

The QueryInterface method provides the IUnknown :: QueryInterface method of a COM object that tries to get a specific interface pointer. [...] To get the IntPtr value that IUnknown represents, you can call Marshal.GetComInterfaceForObject , Marshal.GetIUnknownForObject , or Marshal.GetIDispatchForObject .

I believe you are looking for the Marshal.GetComInterfaceForObject() method.

+4
source

Source: https://habr.com/ru/post/1340920/


All Articles