Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Disable marshalling delegates as _Delegate and enable marshalling delegates as IDispatch.#23738

Merged
jkoritzinsky merged 3 commits intodotnet:masterfrom
jkoritzinsky:delegate-interface-marshalling
Apr 5, 2019
Merged

Disable marshalling delegates as _Delegate and enable marshalling delegates as IDispatch.#23738
jkoritzinsky merged 3 commits intodotnet:masterfrom
jkoritzinsky:delegate-interface-marshalling

Conversation

@jkoritzinsky
Copy link
Copy Markdown
Member

As part of the transition to .NET Core, we dropped the concept of the mscorlib tlb interfaces. However, in the marshalling space we continued to imply that their usage was safe, particularly with delegates. In .NET Framework, it is possible to marshal a delegate as a COM object implementing the _Delegate interface provided by the mscorlib.tlb type library. Since we did not make many significant changes to the System.Delegate type in .NET Core, this behavior continued to work in .NET Core. However, when the System.Delegate type was moved to the shared partition in #23552, the change to the compiled type's surface was large enough to change how the dispid's were assigned, breaking the marshalling to the Framework _Delegate interface and resulting in bad runtime errors (crashes, incorrect return values depending on exactly how the interface pointer is generated).

We've decided that since the _Delegate COM interface is a .NET Framework type, that we are not going to try to add back support to marshalling to it. Instead, we are changing behavior as follows:

  • Using [MarshalAs(UnmanagedType.Interface)] on a delegate-typed field, parameter, or return value is illegal.
  • Using the default marshallling for delegates on parameters or return values in non-WinRT COM methods is illegal since it implies [MarshalAs(UnmanagedType.Interface)].
  • Using [MarshalAs(UnmanagedType.IDispatch)] on a delegate-typed field, parameter or return value is now legal and is the suggested change for consumers. Additionally, these consumers will have to change their native code to take an IDispatch pointer and operate on the delegate's COM wrapper via the IDispatch interface instead of the _Delegate interface.

Fixes #23730.

@jkoritzinsky jkoritzinsky added area-Interop os-windows breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. labels Apr 4, 2019
@jkoritzinsky jkoritzinsky added this to the 3.0 milestone Apr 4, 2019
@jkoritzinsky jkoritzinsky force-pushed the delegate-interface-marshalling branch from 394e51a to 8c7a3f3 Compare April 4, 2019 20:43
Comment thread src/vm/fieldmarshaler.cpp Outdated
@jkoritzinsky jkoritzinsky merged commit 8241971 into dotnet:master Apr 5, 2019
@jkoritzinsky jkoritzinsky deleted the delegate-interface-marshalling branch April 5, 2019 05:57
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…egates as IDispatch. (dotnet/coreclr#23738)

* Disable marshalling delegates as _Delegate and enable marshalling delegates as IDispatch.

* ifdef out the new IDispatch marshalling on non-COM-supporting platforms.

* PR feedback.


Commit migrated from dotnet/coreclr@8241971
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Interop breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. os-windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants