I've been looking for an example of how to implement IUnknown in C#, but haven't found any decent references or solutions to this.
Should it be as simple as...
public interface IUnknown
{
UInt32 AddRef();
UInt32 QueryInterface([In] IntPtr riid, [Out] IntPtr ppvObject);
UInt32 Release();
}
...or is there more to it?
Why would you want to implement IUnknown? That's a COM interface. If you want to interoperate with COM, use the .Net/COM bridge, which implements IUnknown for you.
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISomeInterface
{
...
}
This attribute does ISomeInterface : IUnknown { ... }
Asked was: { ... Just out of interest, the Direct2D interface, ID2D1Factory inherits from IUnknown, so presumably I would still need to implement this interface with .NET...I'm not sure what I would do here exactly. – series0ne May 24 '13 at 8:51 ... } And the answer is no. The whole point of deriving a class from IUnknown is so that your class does not have to implement anything IUknown does for you. It will take care of things like QueryInterface and AddRefCount automatically. You don't do a thing in C# or C++ even. In C++ you then CoCreateInstance(), and in C# it is Activator.CreateInstance().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With