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# console app (CSharpConsole.csproj) and a WinRT Component (WinRTComponent.vcxproj).
You can learn more about Registration-free WinRT here.
The C# desktop app uses:
- The Microsoft.VCRTForwarders.140 NuGet package to manage C++ Runtime dependencies for the C++ component. The GitHub repo can be found here.
Requirements
The sample requires Windows 10 Version 1903.
Instructions
C# app
A non-packaged C# console app referencing a C++ WinRT Component
-
The C# app has a visual Studio reference to the WinRTComponent Project. To add a VS reference to a project right click on the project, then select Add, Reference, Projects, then select the target project to reference.
-
The specific DLLs and classes (from the WinRT component) being referenced should be declared in the Win32 Application manifest - CSharpConsoleApp.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 Post Build Event to copy the component DLL to the app's output directory:
copy /Y "$(SolutionDir)WinRTComponent\bin\$(Platform)\$(Configuration)\WinRTComponent.dll" "$(SolutionDir)$(MSBuildProjectName)\$(OutDir)WinRTComponent.dll"
- To add your own Post Build Event to your C# project right click on the project, then select Properties, Build Events, Post Build Event Command line
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 CSharpConsoleApp project
- Run the app