C++/WinRT:MIDL 3.0 类型:Char - 编译生成的代码 returns 'T must be WinRT type'。有支持使用吗?

C++/WinRT: MIDL 3.0 type: Char - compile of generated code returns 'T must be WinRT type'. Is there a support usage?

使用包含 MIDL 3.0 简单类型字符的 idl 源:

namespace BrokenMIDL_Char
{
    [default_interface]
    runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
    {
        MainPage();
        Int32 MyProperty;
        Char MyStationLetter_1;
        //Char MyStationLetter_2;
        //Char MyStationLetter_3;
    }
}

及配套功能

    wchar_t MyStationLetter_1()
    {
        throw hresult_not_implemented();
    }

    void MyStationLetter_1(wchar_t /*value*/)
    {
        throw hresult_not_implemented();
    }

     char16_t MyStationLetter_2()
    {
        throw hresult_not_implemented();
    }

    void MyStationLetter_2(char16_t /*value*/)
    {
        throw hresult_not_implemented();
    }


    char MyStationLetter_3()
    {
        throw hresult_not_implemented();
    }

    void MyStationLetter_3(char /*value*/)
    {
        throw hresult_not_implemented();
    }

结果出错

T must be a WinRT type

我的理解是 MIDL 3.0 类型是 WinRT 类型的定义。

MIDL 编译器发出:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
template<typename TDeclaringType, typename TValue>
void SetValueTypeMember_MyStationLetter_1(
    ::winrt::Windows::Foundation::IInspectable const& instance,
    ::winrt::Windows::Foundation::IInspectable const& value)
{
    instance.as<TDeclaringType>().MyStationLetter_1(::winrt::unbox_value<TValue>(value));
}

在测试过程中,我有一个解决方案,你可以试试。

您可以打开 属性 选项卡 > 属性 > 配置属性 > C/C++ > 语言,找到Treat Wchar_t As Built in Type 属性 和 select No(/Zc:wchar_t-) 选项。然后,尝试构建您的项目。