Skip to content

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

@darkhz

Description

@darkhz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions