VS 2017 & 2019 cannot find the WinRT header files, even on a fresh solution

5.6k views Asked by At

So I'm trying to update a Win32 application to UWP using a "Windows Application Packaging Project", I'm hitting filesystem issues, which I expected and am trying to resolve them. In order to do this, I need access to the WinRT libraries so that I can deal with UWP paths to get at my assets and resources.

I installed the C++/WinRT VSIX extension and added the NuGet package to my solution. I was assuming from what i read that would be all I needed to do to access the header files. However the compiler apparently cannot find the header files for WinRT and it's namespaces, as I get messages saying cannot open source file "winrt/Windows.Storage.h and I can't use the Windows::Storage::StorageFolder calls I apparently need to get my file system code to work. Despite my best efforts to work out what include paths I'm missing, nothing has worked, so I tried a different tactic.

In the same solution I created a "DirectX 12 App (Universal Windows)" thinking that i may have been going down the wrong route and opted to see if i could re-implement my front end as a straight UWP app without the legacy win32. This fresh project seems to have the right includes as the using directives work for the Windows.* namespaces it uses, and it compiles. Despite this, i can't work out what it's doing to get access namespaces similar to the ones I need, so i can't reverse engineer it to get my original project working. However, I am sceptical if this is using the same libraries, as this project type was available since before I installed the C++/WinRT VSIX extension, and WinRT is not mentioned anywhere.

In order to go back to basics, I created a fresh solution with a "Windows Desktop Application (C++/WinRT)" as i know this uses the runtime I'm after, and it should be clean (as opposed to my long-used solution with tons of projects and configurations) so i can see the options it's using for me to replicate and access the tools i need. However, freshly created, the project will not compile and it cites exactly the same reason as my original solution and project about not being able to find the WinRT headers. This baffled me, so i assumed that it was some other config I'd done globally to VS2017.

To isolate this, I installed VS2019 and added the extension to it, thinking "new environment, no config screwups". But again a freshly created "Windows Desktop Application (C++/WinRT)" project would not build and it could not find the header files.

The autogenerated pch.h contains the following

//it can't find anything starting with "winrt/"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Hosting.h>
#include <winrt/Windows.UI.Xaml.Media.h>
//but it finds this one totally fine
#include <Windows.UI.Xaml.Hosting.DesktopWindowXamlSource.h>

So this is happening in both old and new solution files, and both VS2017 and VS2019. I am officially stumped at what is going on.

Can anyone shed some light as to what I'm missing, so i can get either my win32 app, or freshly created WinRT project to build?

3

There are 3 answers

1
mishmashru On BEST ANSWER

this has been fixed by https://github.com/microsoft/cppwinrt/pull/781 Update your MSIX and NuGet packages

4
Abhishek Sharma On

you have to add a reference to windows .winmd installed somewhere

C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

or somewhere in vs directory by browsing in the reference window then

you can use "using Windows.Storage" that will include the necessary thing I am not sure but I have a winform app in which I used this and it worked fine so maybe you can give it a shot

1
Daniel Baker On

I got the same error message which I solved by adding

%WindowsSdkDir%Include\WindowsTargetPlatformVersion\cppwinrt to the include path or by building the solution with just the include statements which also generates the headers and makes the winrt namespace work.

%WindowsSdkDir% can be obtained by opening a developer command prompt from the menu tools, command line and typing echo %WindowsSdkDir%,

the windowsTargetPlatformVersion can be copied from configuration properties - Windows SDK Version

It is explained at this link

https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/get-started#important-apis