1

I have a pre-existing native C++ library I am trying to expose to UWP applications by projecting the API through a C++/WinRT Runtime Component. I am having difficulty understanding how to expose templated functions.

For the following example C++ class:

struct ExampleClass
{
  ExampleClass() = default;

  template <typename T>
  std::vector<T> create_a_vector();
};

This example is obviously silly; in reality the templated factory is creating another library-specific type (not std::vector), and there are reasons why it's a member of ExampleClass.

I'm hoping to expose a subset of specializations of create_a_vector by specifically declaring and implementing them in my IDL and Runtime Component. For example, the following IDL would be ideal:

namespace ExampleClassWinRT
{
    [default_interface]
    runtimeclass ExampleClass
    {
        ExampleClass();
        IVector<String> CreateAVector<String>();
        IVector<Int32> CreateAVector<Int32>();
        IVector<UInt8> CreateAVector<UInt8>();
        IVector<ExampleClassWinRT.SomeOtherType> CreateAVector<ExampleClassWinRT.SomeOtherType>();
        // .. and so on
    }
}

And then in the implementation (in C++/WinRT) of each of the versions of CreateAVector I would take care of converting between WinRT and C++ types as needed and invoking the correct specialization of create_a_vector.

Then, consuming C# code might look something like this:

using ExampleClassWinRT;
namespace ExampleApp
{
    public sealed partial class MainPage : Page
    {
        public MyFunc()
        {
             var c = ExampleClassWinRT.ExampleClass();
             var v1 = c.CreateAVector<String>();
             var v2 = c.CreateAVector<Int32>();
             var v3 = c.CreateAVector<ExampleClassWinRT.SomeOtherType>();
        }
    }
}

It seems like the syntax used in the goal C# example code is legal, and in fact many WinRT APIs appear templated (IVector<T> for example). However my MIDL syntax above is not valid, and I'm not finding any documentation on how I might create a templated WinRT API surface through MIDL and/or WinRT/C++.

Is this approach for projecting a native C++ library into WinRT even feasible? Am I barking down the wrong tree?

Sean Kelly
  • 338
  • 1
  • 10
  • 1
    Since posting, I've discovered that the proper term to be googling is 'parameterized type', not 'templated type'. Found some links that seem to suggest what I'm going for is explicitly disallowed: https://stackoverflow.com/questions/9509099/winrt-reason-for-disallowing-custom-generic-types-or-interfaces https://learn.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#general-notes – Sean Kelly Nov 27 '19 at 19:41
  • 1
    You cannot use this undefined type(e.g. CreateAVector()) directly in the idl file, it is invalid. If you want to use the native C++ library Api in UWP application, you can link your c++ library into UWP. For more details, you can refer to this [document](https://learn.microsoft.com/en-us/cpp/porting/how-to-use-existing-cpp-code-in-a-universal-windows-platform-app?view=vs-2019#to-use-a-native-c-static-library-in-a-uwp-project). – Faywang - MSFT Nov 28 '19 at 02:54

0 Answers0