Request: COMWrappers Source Generation sample for DeckLink

Ask software engineering and SDK questions for developers working on Mac OS X, Windows or Linux.
  • Author
  • Message
Offline
User avatar

roger.magnusson

  • Posts: 3652
  • Joined: Wed Sep 23, 2015 4:58 pm

Request: COMWrappers Source Generation sample for DeckLink

PostMon Apr 29, 2024 10:22 am

With .NET 8, Microsoft introduced COM Source Generation for interacting with COM-based APIs. This is different from the runtime's built-in COM interop that's normally used (and different from the COMWrapper introduced with .NET 5 that requires that you create a lot of boilerplate code). The upside of the new model is that it allows compiling to Native AOT, meaning you can compile your C# application as a native executable or library that doesn't require .NET at all.

I wonder if there's any interest from the BMD developers to update one of the DeckLink C# code samples in the SDK to use this technology. I'll take a stab at it myself but I'm a C# developer without experience in COM so it's a bit of a struggle. Ultimately I guess this would require another dll like DeckLinkAPI64.dll but with the members decorated with the new attributes for source generation.

Here's a talk from Microsoft on COM Source Generation for .NET 8.

The new stuff starts at 14:37 in the video.

Here's the documentation:
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation
Offline
User avatar

roger.magnusson

  • Posts: 3652
  • Joined: Wed Sep 23, 2015 4:58 pm

Re: Request: COMWrappers Source Generation sample for DeckLi

PostThu May 02, 2024 7:23 pm

After some testing I find the new source generator for COM is pretty great in terms of ease of use and the ability to compile to native binaries. Enumerating DeckLink devices works without issues but it's throwing an exception when I try to set a capture callback.
Code: Select all
System.InvalidCastException: 'Unable to cast the provided managed object to a COM interface with ID '{c6fce4c9-c4e4-4047-82fb-5d238232a902}''
So perhaps the DeckLink API will require some custom marshalling in a few places, I haven't looked into that yet.


EDIT:
Ah, I made a mistake. When creating your own InputCallback class that implements the IDeckLinkInputCallback interface, it too has to be decorated with the [GeneratedComClass] attribute. Then it works. :)

Some pain points that you have to deal with manually as far as I know:
  • COM interop methods that return an int (or an enum that's an int) have to be decorated with the [PreserveSig] attribute, or it will assume the return type is an HRESULT and mess everything up.

  • COM interop methods that have the extern modifier will create a warning when compiling to Native AOT, saying it will always throw an exception because the IL is invalid.

    I'm not sure what the proper solution is but I changed this:
    public virtual extern void Next(out IDeckLink deckLinkInstance);

    To this:
    public virtual void Next(out IDeckLink deckLinkInstance)
    {
        // Won't be thrown - the source generator hides this implementation with the new modifier
        throw new NotImplementedException();
    }

Now I have everything I need for compiling C# .NET 8 to fast native binaries with no external dependency on .NET at all.
Offline

SkinnyBiker

  • Posts: 1
  • Joined: Tue May 14, 2024 7:38 am
  • Real Name: Matthijs Hornix

Re: Request: COMWrappers Source Generation sample for DeckLi

PostTue May 14, 2024 7:45 am

Hi Roger, I think a COM source generation sample would be great as well!
I tried to implement the same as you before but was unable to do so, do you mind sharing a small example or GitHub repo to maybe help me and others struggling to do what you succeeded in already?
Thanks in advance!
Offline
User avatar

roger.magnusson

  • Posts: 3652
  • Joined: Wed Sep 23, 2015 4:58 pm

Re: Request: COMWrappers Source Generation sample for DeckLi

PostMon May 27, 2024 7:49 am


Return to Software Developers

Who is online

Users browsing this forum: No registered users and 5 guests