具有非原始函数 return 类型的 WinRT C++ 组件无法 compile/build

WinRT C++ component with non primitive function return types fails to compile/build

下面的程序在具有原始 return 类型函数的函数时工作正常,特别是下面定义的 Pop 函数可以正常工作并且我能够正确构建,但是当我使用具有非原始函数的函数构建解决方案时return 像下面这样的 PopC 类型的项目没有构建,但定义在语法上是正确的,我能够看到在 pch.h 文件中使用正确的命名空间定义正确引用的类型。

在StreamSamplePool.idl文件中定义了以下代码,

    namespace WinRTComponent
    {
    [default_interface]
    runtimeclass StreamSamplePool
    {
        StreamSamplePool();
        Int32 MyProperty;
       // MediaStreamSample PopC();
        Int32 Pop();
    }
    }

并且在StreamSamplePool.cpp文件里面定义了各自的方法如下

        #include "pch.h"
        #include "StreamSamplePool.h"
        #include "StreamSamplePool.g.cpp"
        #include <stack>
        #include <queue>
        using namespace winrt::Windows::Storage::Streams;
        using namespace  winrt::Windows::Foundation;
        using namespace std;
        //using namespace Platform;
        using namespace  winrt::Windows::Media::Core;
    
        namespace winrt::WinRTComponent::implementation
        {
        int32_t StreamSamplePool::MyProperty()
        {
            throw hresult_not_implemented();
        }
        int32_t StreamSamplePool::Pop()
        {
            return int32_t();
        }
        //MediaStreamSample StreamSamplePool::PopC()
        //{
        //    MediaStreamSample sample = MediaStreamSample::CreateFromBuffer(NULL, 
         //TimeSpan());
        //    //sample.Processed += OnSampleProcessed;
        //    return sample;
        //}
        void StreamSamplePool::MyProperty(int32_t /* value */)
        {
            throw hresult_not_implemented();
        }
        }

并且在StreamSamplePool.h

里面定义了如下代码
        namespace winrt::WinRTComponent::implementation
        {
        struct StreamSamplePool : StreamSamplePoolT<StreamSamplePool>
        {
            StreamSamplePool() = default;
    
    
            int32_t Pop();
    
            //MediaStreamSample PopC();
    
            int32_t MyProperty();
            void MyProperty(int32_t value);
        };
        }
    
        namespace winrt::WinRTComponent::factory_implementation
        {
        struct StreamSamplePool : StreamSamplePoolT<StreamSamplePool, 
       implementation::StreamSamplePool>
       {
       };
       }

错误信息

Error MIDL2011 [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass 'WinRTComponent.StreamSamplePool' ) ] WinRTComponent D:\Prithvi\Work\WinRTComponent\StreamSamplePool.idl 9

编译器输出

1>D:\...\StreamSamplePool.idl(9): error MIDL2011: [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass 'WinRTComponent.StreamSamplePool' ) ]
1>Done building project "WinRTComponent.vcxproj" -- FAILED.

问题可以简化为以下 IDL 文件:

namespace WinRTComponent
{
    [default_interface]
    runtimeclass StreamSamplePool
    {
        StreamSamplePool();
        MediaStreamSample PopC();
    }
}

使用 default-generated C++/WinRT 组件项目编译它会产生以下错误:

1>C:\...\StreamSamplePool.idl(7): error MIDL2011: [msg]unresolved type declaration [context]: WinRTComponent.MediaStreamSample [ Procedure 'PopC' ( RuntimeClass 'WinRTComponent.StreamSamplePool' ) ]

它包含解决问题所需的所有信息。特别是它命名错误(“未解析的类型声明”)和受影响的过程(“PopC”)。要解决这个问题,您必须在 IDL 文件中提供 fully-qualified 类型名称,即

namespace WinRTComponent
{
    [default_interface]
    runtimeclass StreamSamplePool
    {
        StreamSamplePool();
        Windows.Media.Core.MediaStreamSample PopC();
    }
}

完成后,进入项目根目录下的“生成Files\sources”目录,将[中的相应部分复制]。 =34=]""StreamSamplePool.cpp" 构建系统生成到您的源文件的文件。