dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.07k stars 4.69k forks source link

Issue with COM Interop in VB6 After Migrating from .NET 4.8 to .NET 8 #103656

Closed masterprogrammer513 closed 2 months ago

masterprogrammer513 commented 3 months ago

I am facing a problem with COM interop in VB6 after migrating from .NET 4.8 to .NET 8. This setup used to work perfectly in .NET 4.8 but is now failing in .NET 8.

Here's the setup:

I have a BaseClass in ASM1 defined as BaseClass : IBaseClass. There is another assembly, ASM2, which has a method like Method(BaseClass baseClass). Both classes are properly implemented with ComVisible attributes and have the necessary GUIDs, etc.

Here is the problematic VB6 code:

vb6 Copy code

Dim baseCls As ASM1_BaseClass
Set baseCls = New ASM1_BaseClass

Dim cls2 As ASM2_Class2
Set cls2 = New ASM2_Class2
cls2.Load(baseCls) ' This line throws InvalidCastException: "Cannot cast BaseClass to BaseClass"
The line cls2.Load(baseCls) throws an InvalidCastException with the message "Cannot cast BaseClass to BaseClass".

Has anyone encountered a similar issue when migrating from .NET 4.8 to .NET 8? How can I resolve this casting problem?

This is a sample project

https://github.com/masterprogrammer513/Com-Net8-Problem

image

net48 assembly load image

Net8 assembly load image

.net8 loads a PrentCom multiple times in different contexts which makes a casting problem

masterprogrammer513 commented 3 months ago

Any news?

AaronRobinsonMSFT commented 2 months ago

@masterprogrammer513 COM components are now always isolated. This behavior can be altered as described here. Ideally this would handled in a more precise manner by directly managing AssemblyLoadContexts.