Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes to document COM interface inheritance #192

Closed
rickbrew opened this issue Sep 7, 2021 · 1 comment · Fixed by #193
Closed

Add attributes to document COM interface inheritance #192

rickbrew opened this issue Sep 7, 2021 · 1 comment · Fixed by #193
Labels
proposal An issue that represents a proposed feature or change to the repo. untriaged An issue that has not been triaged by the repo maintainers.

Comments

@rickbrew
Copy link
Contributor

rickbrew commented Sep 7, 2021

We've talked about this on Discord, but having an issue will make this easier to track.

COM interfaces have inheritance relationships, e.g. ID2D1SolidColorBrush inherits from ID2D1Brush, then ID2D1Resource, then IUnknown. In native code you can pass an ID2D1SolidColorBrush* to a method that takes an ID2D1Brush* and it works fine. With C# / TerraFX, you have to cast the pointer every time you do this. The cast is safe, but the compiler can't verify that, and you can easily do the wrong thing such as casting ID2D1Bitmap* to ID2D1Brush*. It'll crash spectacularly at runtime, but nothing at compile-time.

Making this work in TerraFX would be an uphill battle since C# doesn't have any way of making this work, other than maybe auto-generating e.g. struct PIUnknown { IUnknown *p; } wrappers for every interface along with the proper cast operators. And then all your code has to use these in place of pointers, but also needs to use the real pointers at ABI boundaries.

TerraFX doesn't need to include these wrappers and the casting operators, but I could generate them on my own for my own project. However, I can't do that without the inheritance information, which doesn't exist.

So, something like this would be nice in TerraFX:

[NativeInheritance(typeof(ID2D1Brush))]
public struct ID2D1SolidColorBrush
{
    ...
}

Then I can experiment with code-genning my own solutions to this. Anything easily accessible via standard reflection should work.

Some interfaces have multiple interfaces they derive from. Those can be handled by using additional [NativeInheritance(...)]'s.

[NativeInheritance(typeof(IBaseInterface1))]
[NativeInheritance(typeof(IBaseInterface2))]
public struct IMultipleInheritance
    // : IBaseInterface1, IBaseInterface2
{
    ...
}

I'd prefer typeof over strings, e.g. [NativeInheritance("ID2D1Brush")]

@rickbrew rickbrew added proposal An issue that represents a proposed feature or change to the repo. untriaged An issue that has not been triaged by the repo maintainers. labels Sep 7, 2021
@tannergooding
Copy link
Member

This should be mostly covered by #193. It is currently using a string as I've not updated ClangSharp to support emitting typeof yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal An issue that represents a proposed feature or change to the repo. untriaged An issue that has not been triaged by the repo maintainers.
Projects
None yet
2 participants