Вопрос

I have a COM interface defined like this

interface Client : IUnknown
{
    [id(1)] HRESULT GetSomething
    (
        enum SomeID someID,
        [out] IUnknown  **pUnknown
    );
};

How can I pass IUnknown object using C#?

Это было полезно?

Решение

You don't pass anything, it returns an interface pointer. An opaque one, IUnknown is implemented by all COM interfaces. In C# it will be an object of type object, the managed version of "could be anything". So basic code looks like this:

object retval;
client.GetSomething(SomeID.Magic, out retval);

And then it is up to you to cast the returned object into the actual interface type, probably based on Magic. Your question gives no hint at all what it might be, you'll have to read the documentation of the COM component to have an idea.

Другие советы

Assuming you don't know anything about the object, you would probably use an IntPtr to represent the IUnknown and then get a RCW for it using Marshal.GetUniqueObjectForIUnknown:

// Get some object

IntPtr ptrObj = IntPtr.Zero;


Client.GetSomething(someID, out ptrObj);
object obj = Marshal.GetUniqueObjectForIUnknown(ptrIProfferService);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top