TJA
TJA

Reputation: 3051

How do I fix Visual Studio 2022 Error E1696 for WinRT

When I generate a new WinRT project in Visual Studio 2022 I get Error E1696 cannot open source file "winrt/Windows.Foundation.h" yet when I look at the Include directories the files do exist at the correct location.

Upvotes: 2

Views: 11909

Answers (3)

Richard Jessop
Richard Jessop

Reputation: 972

I had a similar problem for an example program that I downloaded which was from a web page that was pretty dated. The E1696 problem was not being able to find stdio.h.

Bottom line: Right mouse click on the project>Retarget Projects.

Upvotes: 0

IInspectable
IInspectable

Reputation: 51506

This is an artifact of the way C++/WinRT works. While the header files do exist in the Windows SDK, that's not where the project goes looking for them. Instead, they are generated on the fly into the source tree under the Generated Files directory.

So to fix the issue you will have to compile a newly created project at least once. This by itself isn't sufficient for IntelliSense to pick up the changes in environment. To help IntelliSense out you're going to have to right-click into the source editor, and select Rescan -> Rescan File.

Once that is done, all the IntelliSense errors go away, including E1696.


Historic background

It's easy to get confused why the C++/WinRT header files are part of the Windows SDK, yet the C++/WinRT VSIX templates aren't using them. A look back at C++/WinRT's history helps explain how we landed in this situation:

Initially, the code generator responsible for producing the language projection header files (modern.exe, later renamed to cppwinrt.exe) wasn't published. Instead, the header files were generated by Kenny Kerr, and published through his modern repo.

Publishing the language projection header files through a GitHub repository carried over into the cppwinrt repo owned by Microsoft, and continued to be the deployment channel for several versions of Windows.

This wasn't exactly convenient for developers, so with the release of the Windows SDK for Windows 10, version 1803 (10.0.17134.0) the headers got added to the SDK (including the code generator). This worked, but wasn't an ideal situation either, as the release cycle of C++/WinRT was now tied to that of the Windows SDK, roughly 6 months.

Decoupling the release cycles was crucial in allowing C++/WinRT to progress at its own pace, shipping frequent updates with improvements and bug fixes. This was enabled by deploying the code generator as part of a NuGet package that the C++/WinRT project templates reference. The MSBuild project drives generation of the language projection headers, and clients can freely decide, which version of the C++/WinRT library they wish to use, controlled through the NuGet package reference.

This is how things work today, but the language projection headers can no longer be yanked from the Windows SDK. They were published, with clients relying on finding them there, and expecting an SDK update to not break their builds. And this is why the Windows SDK contains header files you aren't meant to be using.

Upvotes: 8

TJA
TJA

Reputation: 3051

Often a Build --> Clean Solution followed by a Build --> Build Solution is enough to resolve the issue. Give Visual Studio a few seconds to complete any background work.

If that fails try reinstalling the Microsoft.Windows.CppWinRT NuGet package.

  1. Go to Tools --> NuGet Package Manager --> Manage NuGet Packages For Solution...
  2. In the NuGet Solution pane choose the Installed option.
  3. Tick the CheckBox next to your Project name in the Window to the right.
  4. Click the Uninstall button
  5. And click Ok in the Change Preview dialog box.
  6. The CPPWinRT package will now be removed.
  7. Change to the Browse option in the NuGet Solution pane.
  8. Type "cppwinrt" into the Search text box of the pane.
  9. Select the Microsoft.Windows.CppWinRt package and install it.
  10. Finally choose the Build --> Build Solution option.
  11. After the Build has been completed give Visual Studio a few more seconds to complete any background work and the errors should be gone.

Upvotes: 0

Related Questions