SpiritBob
SpiritBob

Reputation: 2702

Issues with COM between a .NET 8+ project and a .NET Framework one

I tried making a .NET 8+ COM Host whose interface has an IEnumerable that under the hood is an iterator method utilizing yield statements, to be used from a .NET Framework v4.8 project.

My interface is the following:

    [ComVisible(true)]
    [Guid("<random-guid>")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IDemoReader
    {
        IEnumerable<string> GetData();
    }

I somehow got to the point where my .NET Framework program made an instance of IDemoReader, but the return type I had was string[], as I had to manually create a .tlb file from C++, and then used another tool called tlbimp that generated me a .dll out of my .tlb which I could reference in the .NET Framework application.

That .dll for some reason changed the return type to be a string[] instead of IEnumerable<string>. I'm guessing generics are a no-no?

The moment the method was called, the .NET Framework process terminated with the following error:

Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'return value': Non-blittable generic types cannot be marshaled.
   at DemoReader.Interop.IDemoReader.GetData()
   at Tester.Framework.Program.Main(String[] args)

Upvotes: 0

Views: 162

Answers (0)

Related Questions