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