Home > Enterprise >  How do I fix Visual Studio 2022 Error E1696 for WinRT
How do I fix Visual Studio 2022 Error E1696 for WinRT

Time:12-19

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.

CodePudding user response:

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.

CodePudding user response:

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.
  • Related