1

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?

1 Answers1

1

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • I see. I just started with Windows development, and didn't know midl can be used without WinRT. – Balázs Kovacsics Feb 26 '20 at 21:26
  • Is there any way I could define an enum in plain midl, with some cpp_quotes, and use it in a WinRT runtimeclass? – Balázs Kovacsics Feb 26 '20 at 21:27
  • 1
    @bal: No. If your enum shows up in the signature of a Windows Runtime type, you'll have to declare it in IDL and have MIDL generate the respective metadata. If the enum is internal to the implementation, then there's no need to put it into the IDL. – IInspectable Feb 26 '20 at 21:44