c++c++-winrtmidl

Why doesn't `cpp_quote` and `pragma midl_echo` used in a .idl file output anything to the generated header in a C++/WinRT project?


I'm trying to add some C++ code to the generated header coming form the MIDL compiler, using the cpp_quote attribute. But nothing gets added to the generated output, and I have no idea why.

I use VS 2019 with the C++/WinRT extension, the project is a C++/WinRT project with the default config.

Input IDL file:

cpp_quote("#define FOO")

namespace FooWinRT
{
    runtimeclass Class
    {
        Class();
        Int32 MyProperty;
    }
}

Generated output:

// WARNING: Please don't edit this file. It was generated by C++/WinRT v2.0.200224.2

#pragma once
#include "winrt/FooWinRT.h"
namespace winrt::FooWinRT::implementation
{
    template <typename D, typename... I>
    struct __declspec(empty_bases) Class_base : implements<D, FooWinRT::Class, I...>
    {
        using base_type = Class_base;
        using class_type = FooWinRT::Class;
        using implements_type = typename Class_base::implements_type;
        using implements_type::implements_type;

        hstring GetRuntimeClassName() const
        {
            return L"FooWinRT.Class";
        }
    };
}
namespace winrt::FooWinRT::factory_implementation
{
    template <typename D, typename T, typename... I>
    struct __declspec(empty_bases) ClassT : implements<D, Windows::Foundation::IActivationFactory, I...>
    {
        using instance_type = FooWinRT::Class;

        hstring GetRuntimeClassName() const
        {
            return L"FooWinRT.Class";
        }
        auto ActivateInstance() const
        {
            return make<T>();
        }
    };
}

#if defined(WINRT_FORCE_INCLUDE_CLASS_XAML_G_H) || __has_include("Class.xaml.g.h")
#include "Class.xaml.g.h"
#else

namespace winrt::FooWinRT::implementation
{
    template <typename D, typename... I>
    using ClassT = Class_base<D, I...>;
}

#endif

(Of course the actual production code is more complex, this is just a minimal example of what I'm trying to do.)

What am I missing, why is it not doing what it's supposed to do?


Solution

  • The header "was generated by C++/WinRT v2.0.200224.2", i.e. cppwinrt.exe, not midl[rt].exe. The latter produces a .winmd file that's used by the former to emit the implementation header. I'm not aware of a way to instruct the tools to make cpp_quotes show up in the implementation headers.