microsoft / microsoft/CsWinRT

Bug: [CSWinRT 3]: Crash when project is built as a NativeAOT library

Open
#2,441 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Description

When a project is set as PublishAot = true, and the OutputType = Library, and when trying to dynamically load said library within another application and call WinRT related functions, the following exception is thrown:

Unhandled exception. System.NotSupportedException: The managed type 'System.Object' does not have any associated marshalling info. This should never be the case
. Please file an issue at https://github.com/microsoft/CsWinRT.
   at WindowsRuntime.InteropServices.WindowsRuntimeComWrappers.GetOrCreateComInterfaceForObject(Object) + 0x1dd
   at WindowsRuntime.InteropServices.Marshalling.WindowsRuntimeInterfaceMarshaller`1.ConvertToUnmanaged(T, Guid&) + 0xa3
   at ABI.Windows.Devices.Enumeration.IDeviceInformationStatics2Methods.FindAllAsync(WindowsRuntimeObjectReference, String, IEnumerable`1, DeviceInformationKind
) + 0xca
...

Otherwise, when the same project is compiled as an Exe, there are no exceptions and it works.

I currently do not have a minimal repro, although I will try to provide one.

Steps To Reproduce
  1. Prepare a project of type OutputType=Library, and set PublishAot=true.
  2. Export a simple function (using UnmanagedCallersOnly) within the project which calls any WinRT-related function or component (for example, DeviceInformation.FindAllAsync()).
  3. Compile the project.
  4. Dynamically load the built NativeAOT library into any other application, and call the exported function.
Expected Behavior

The application does not throw an exception.

Version Info

CsWinRT version: 3.0.0-preview260319.2
NET SDK: net10.0-windows10.0.19041.1

Additional Context

Specifically, what I have observed is:

  • The TypeMapAssemblyTarget generator usually generates all the assembly attributes required for the application to function when the OutputType=Exe and PublishAot=true.
  • When OutputType=Library and PublishAot=true, the generator is disabled, and the project compiles without the necessary assembly attributes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the TypeMapAssemblyTarget-generated assembly attributes for PublishAot projects built as Exe versus Library, using the UnmanagedCallersOnly and DeviceInformation.FindAllAsync reproduction described in the issue. Verify the dynamically loaded NativeAOT library can call WinRT functions without the marshalling exception, and add a regression test if the project’s existing tests support this scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.