3.3 KiB
Registration-free WinRT in C++ Desktop App Sample
This sample demonstrates how to use Registration-free WinRT a new feature in Windows 10 Version 1903 to reference a C++ Windows Runtime component in a non-packaged C++ desktop app. It contains a C++ desktop app (CppConsoleApp.vcxproj) and a WinRT Component (WinRTComponent.vcxproj).
You can learn more about Registration-free WinRT here.
The C++ desktop app uses:
- C++/WinRT to create projection header files of the component for use in the app code
- The Microsoft.VCRTForwarders.140 NuGet package to manage C++ Runtime dependencies for the component. The GitHub repo can be found here.
Requirements
The sample requires Windows 10 Version 1903.
Instructions
CppConsoleApp
A non-packaged C++ app referencing a C++ WinRT Component.
- The specific DLLs and classes (from the WinRT component) being referenced should be declared in the Win32 Application manifest - CppConsoleApp.exe.manifest.
To add a new Win32 Application Manifest to a desktop app, right click on the project, then select Add, New Item, General, Application Manifest File, and name it yourappname.exe.manifest.
-
For the System to locate the component DLLs specified in the application manifest at runtime, the DLLs must be in the same directory as the app's .exe. This project uses a propery sheet (PropertySheet.props) to copy the component DLL to the app's output directory.
-
The property sheet is also used to add a reference to the component's WinMD to enable C++/WinRT to generate projection headers of the component which are used in the app code.
-
To add your own property sheet to a C++ project:
- right-click the project node
- select Add, New Item, Visual C++, Property Sheets, Property Sheet (.props)
- edit the resulting property sheet file
- add the necessary project customizations
- select View, Other Windows, Property Manager
- right-click the project node
- select Add Existing Property Sheet...
- select the newly created property sheet file
WinRTComponent
A simple C++ WinRT Component with a class returning a string
- The component is written using C++/WinRT instead of C++/CX. Therefore, it requires an installed Nuget Package: Microsoft.Windows.CppWinRT.
- To manage the specific C++ Runtime Dependencies of the WinRT component, the app uses the Microsoft.VCRTForwarders.140 NuGet package.
Building and running the sample
- Load the Project solution.
- Retarget the solution to the SDK version on your machine: Right click, Retarget solution
- Build the WinRTComponent project.
- Build the CppConsoleApp project
- Run the app