4
votes

Visual Studio 2019. Windows Runtime Component C++/WinRT project.

How do I follow and implement the recommendation: "We recommend that you declare each runtime class in its own Interface Definition Language (IDL) (.idl) file, in order to optimize build performance when you edit an IDL file, and for logical correspondence of an IDL file to its generated source code files. Visual Studio merges all of the resulting .winmd files into a single file with the same name as the root namespace. That final .winmd file will be the one that the consumers of your component will reference."

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-apis

Within the same project I can add a second (third and so on) .IDL file using Project->Add New Item->Midl File (.idl). This seems to work ok?! I then manually add associated .h and .cpp files for each .IDL file. Build to generate stub files then manually copy and "fill in" these files before a final build which is successful. In this way I have a single IDL file per runtime class with its associated headers and implementation files including any other files (c++ .h and .cpp files) needed for the implementation. As I need to add functionality to the interface I just edit the IDL file, rebuild and add functionality to the header and implementation C++ files.

Is this what was recommended?!?

When I add the .IDL file and then manually add an associated .h and .cpp they do not appear "under" the .IDL file as the original ones did when I created the new project. Is this just a visual nicety of Solution Explorer OR is this indicating that I have something wrong?!?

I plan to have multiple "C++/WinRT WRC" projects (one for each namespace, each consisting of multiple runtimeclass interfaces with separate .IDL/.h/.cpp files as described above. Then I reference all of the projects (project to project) in my single C#/UWP App.

I am actually trying to implement a user interface to allow me to utilise a large code base of math/engineering C++ classes that implement a very specific & proprietary type of electronic circuit simulation already existing and programmed in C++ and compiled as a console app. The user data in and out is relatively small compared to the processing that takes place to generate the output.

1
There is no association between an .idl file name and the corresponding .h/.cpp file names of the implementation type, other than the convention of the cppwinrt.exe tool to apply the same base names. The grouping in Visual Studio's Solution Explorer appears to model tooling dependencies (I never saw this for .idl files, though, unlike .xaml files). But even then, changing an .idl always requires you to manually update the implementation type, so automatic dependency tracking wouldn't buy you anything here.IInspectable
I now realise grouping behaviour due to VS (similar to "MainPage.xaml.cs" appearing under "MainPage.xaml" for a C#UWP project). Rename Class.idl then renames Class.h and Class.cpp accordingly. Maybe functionality not yet included (limitation of the Extension) to add more .IDL (other than manually). I maintain naming and manually add more .IDL files with associated .h and .cpp files. "Build All" seems to work ok. Thanks.DefinedRisk

1 Answers

2
votes

It looks like you are handling the idl files correctly and that behavior is the same as I experienced when just adding a new Midl file directly.

If you like the look of having the .h and .cpp files "under" the .idl file in the tree you can try adding a new View Model instead of a new Midl file. This will create the .idl/.h/.cpp files all with the same name and place the .h and .cpp files visually under the Midl file. The idl template you get this way is for a basic runtime class.