Initialize COM object in .Net 8 when using the new GeneratedComInterface and NativeAOT
Hey everyone, I'm trying to NativeAOT my app that uses COM for interaction with WasAPI. I've converted all my code to use the new .Net 8 GeneratedComInterface code generation feature, but I've reached a wall.
I need to initialize a COM object of a particular type ("BCDE0395-E52F-467C-8E3D-C4579291692E") which inherits the interface ("A95664D2-9614-4F35-A746-DE8DB63617E6").
Previous I did this by just specifying the class, doing new() and casting it to the interface - which worked despite the class not inheritting the interface on the C# side. Now with GeneratedComInterface and GeneratedComClass it requires me to pair them together, but I don't have the implementation - I just want to consume the object.
What is the right way to declare the class initialize the object?
2 Replies
you would need to P/Invoke into CoCreateInstance, as you would to create the object in C++
https://learn.microsoft.com/en-us/windows/win32/coreaudio/mmdevice-api
so, something like this in C#:
.
damn yeah that works
I thought I had tried it
but I guess something was subtly different from your snippet