Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: AOT doesn't work with interfaces #1890

Open
lyf6lyf opened this issue Dec 24, 2024 · 3 comments
Open

Bug: AOT doesn't work with interfaces #1890

lyf6lyf opened this issue Dec 24, 2024 · 3 comments

Comments

@lyf6lyf
Copy link

lyf6lyf commented Dec 24, 2024

Description

My code is like below

    public class Parent { }
    public sealed class Child : Parent { }
    public static class Example
    {
        public static Child GetChild() {  return new Child(); }
    }

But it doesn't compile because "error CsWinRT1005: Exporting unsealed types is not supported in CsWinRT, please mark type Parent as sealed"
I use interfaces as workaround, and change my code to

    public interface IParent {}
    public interface  IChild : IParent {}
    internal partial class Parent : IParent {}
    internal partial class Child : Parent, IChild {}
    public sealed class Example
    {
        public static IChild GetChild() { return new Child(); }
    }

And it works well.

Next, I enable AOT following https://github.com/MichalStrehovsky/CppPublishAotReference
When I run the code, I see below error:
Exception thrown at 0x00007FFC28D2837A (KernelBase.dll) in CppConsoleApp.exe: WinRT originate error - 0x80004002 : 'Failed to create a CCW for object of type 'AuthoringDemo.Parent' for interface with IID 'F4D7DFF0-F052-5B09-8FB3-7B563C3D0E1C': the specified cast is not valid.'.
Exception thrown at 0x00007FFC28D2837A (KernelBase.dll) in CppConsoleApp.exe: WinRT originate error - 0x80004002 : 'Failed to create a CCW for object of type 'AuthoringDemo.Child' for interface with IID '58D127C1-2630-5E63-8877-42BC48D395E8': the specified cast is not valid.'.

Steps To Reproduce

  1. clone lyf6lyf@e05e589
  2. build and run \src\Samples\AuthoringDemo\CppConsoleApp\main.cpp

Expected Behavior

The code can run successfully.

Version Info

CsWinRT version: 2.2.0

DotNet version: net8.0-windows10.0.22621.0, net9.0-windows10.0.22621.0

Additional Context

No response

@hez2010
Copy link
Contributor

hez2010 commented Dec 24, 2024

Did you copy the produced winmd files to the directory of your executable file?

@lyf6lyf
Copy link
Author

lyf6lyf commented Dec 25, 2024

Did you copy the produced winmd files to the directory of your executable file?

@hez2010 I just tried but it still doesn't work.

@dongle-the-gadget
Copy link
Contributor

dongle-the-gadget commented Dec 26, 2024

This looks to be a generator miss. Can you confirm if adding this works for you?

[global::WinRT.WinRTExposedType(typeof(global::WinRT.AuthoringDemoVtableClasses.ABI_Impl_AuthoringDemo_ChildWinRTTypeDetails))]
partial class Child
{
}

namespace WinRT.AuthoringDemoVtableClasses
{
    internal sealed class ABI_Impl_AuthoringDemo_ChildWinRTTypeDetails: global::WinRT.IWinRTExposedTypeDetails
    {
        public global::System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry[] GetExposedInterfaces()
        {
            return new global::System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry[]
            {
                new global::System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry
                {
                    IID = global::ABI.AuthoringDemo.IParentMethods.IID,
                    Vtable = global::ABI.AuthoringDemo.IParentMethods.AbiToProjectionVftablePtr
                },
                new global::System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry
                {
                    IID = global::ABI.AuthoringDemo.IChildMethods.IID,
                    Vtable = global::ABI.AuthoringDemo.IChildMethods.AbiToProjectionVftablePtr
                },
            };
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants