Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blank App, Packaged (WinUI3 in Desktop) C++ fails to compile #5041

Open
HUN73R opened this issue Jan 15, 2025 · 5 comments
Open

Blank App, Packaged (WinUI3 in Desktop) C++ fails to compile #5041

HUN73R opened this issue Jan 15, 2025 · 5 comments
Labels
area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration bug Something isn't working needs-triage

Comments

@HUN73R
Copy link
Member

HUN73R commented Jan 15, 2025

Describe the bug

The blank app template for a packaged app using WinUI3 in Desktop (C++) fails to compile with error MIDL2011: [msg]unresolved type declaration [context]: Microsoft.UI.Xaml.Window [ RuntimeClass 'OlMaRudge.MainWindow' ]

The C# template also fails to compile due to a missing assembly, none of the Microsoft.UI.Xaml namespaces are recognized, but in the C# template if I downgrade the Microsoft.WindowsAppSDK nuget package then it appears to have fixed the issue.

Steps to reproduce the bug

  1. Use VS2022 to create a new project using the Blank App, Packaged (WinUI3 in Desktop) C++ template
  2. Build
  3. Fail

Expected behavior

Unmodified template project should not fail to build

Screenshots

No response

NuGet package version

None

Windows version

No response

Additional context

No response

@HUN73R HUN73R added the bug Something isn't working label Jan 15, 2025
@DarranRowe
Copy link

I don't see this.

Is Visual Studio up to date? Are your extensions up to date?

@HUN73R
Copy link
Member Author

HUN73R commented Jan 16, 2025 via email

@karkarl karkarl transferred this issue from microsoft/microsoft-ui-xaml Jan 16, 2025
@RDMacLachlan RDMacLachlan added the area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration label Jan 17, 2025
@DarranRowe
Copy link

Apologies for taking so long to reply.

To tell the truth, I kind of used "extension" to potentially make you stop and think and double check the project. But this particular error has three major components. First is the Windows SDK, the second is C++/WinRT and the third is the Windows App SDK.

The following is taken from the diagnostic log of a build.

C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\midl.exe /metadata_dir "C:\Program Files (x86)\Windows Kits\10\References\10.0.26100.0\windows.foundation.foundationcontract\4.0.0.0" /winrt /W1 /nologo /char signed /env x64 /winmd "x64\Debug\Unmerged\MainWindow.winmd" /h "nul" /dlldata "nul" /iid "nul" /proxy "nul" /notlb /client none /server none /enum_class /ns_prefix /target "NT60"  /nomidl @"x64\Debug\App1.vcxproj.midlrt.rsp" MainWindow.idl

For the default project template, the only time that midl touches both MainWindow and Microsoft.UI.Xaml.Window is when the .winmd file is created for MainWindow. The references are gathered in GetCppWinRTDirectWinMDReferences and written to the file $(IntDir)$(ProjectFileName).midlrt.rsp in CppWinRTSetMidlReferences. So the portion of the build that is failing is touched by C++/WinRT and the Windows SDK.

The .winmd references are also added by the Windows App SDK, so having that up to date (1.6.4) is also useful.

One other thing that I remembered. I think I recall people mentioning issues if the project is under a directory that has spaces in it. Is this the case here?

@HUN73R
Copy link
Member Author

HUN73R commented Jan 21, 2025

I updated my VS22 installer to include the Windows 11 SDK (10.0.26100.0). I did not previously have this latest version of the Windows SDK installed. It did not change anything.

The template project comes set up with WindowsAppSdk 1.7.240912003 in the NuGet packages. That's the latest version. I've tried going back to older versions and that fixed the issue for me with a C# template project which also did not build at first. However, the C++ template project is unaffected by changing the WindowsAppSdk version.

Microsoft CppWinRT package is not installed in the NuGet packages. Perhaps that is what's missing?

@DarranRowe
Copy link

DarranRowe commented Jan 22, 2025

C++/WinRT is required for the C++ projects. There are some modifications that it makes to building, like gathering and providing metadata (.winmd files) for midl. But most importantly:

#pragma once
#include <windows.h>
#include <unknwn.h>
#include <restrictederrorinfo.h>
#include <hstring.h>

// Undefine GetCurrentTime macro to prevent
// conflict with Storyboard::GetCurrentTime
#undef GetCurrentTime

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Microsoft.UI.Composition.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Microsoft.UI.Xaml.Controls.Primitives.h>
#include <winrt/Microsoft.UI.Xaml.Data.h>
#include <winrt/Microsoft.UI.Xaml.Interop.h>
#include <winrt/Microsoft.UI.Xaml.Markup.h>
#include <winrt/Microsoft.UI.Xaml.Media.h>
#include <winrt/Microsoft.UI.Xaml.Navigation.h>
#include <winrt/Microsoft.UI.Xaml.Shapes.h>
#include <winrt/Microsoft.UI.Dispatching.h>
#include <wil/cppwinrt_helpers.h>

This is the default contents of pch.h, the precompiled header for the C++ WinUI project template. Everything that starts with winrt/Microsoft there has to be generated by C++/WinRT. The Windows SDK does supply a more outdated set of headers, but the Windows App SDK only comes with metadata, meaning that the projection must be generated locally.

Since you mentioned this, I removed C++/WinRT from a WinUI 3 project, and it immediately fails with MIDL2011.

Image

So try adding C++/WinRT to the project. I am also aware that if the project templates are generating projects without C++/WinRT then this is only a work around and this should be fixed. However, there is a workaround available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DeveloperTools Issues related to authoring (source and IDL), debugging, HotReload, LiveVisualTree, VS integration bug Something isn't working needs-triage
Projects
None yet
Development

No branches or pull requests

4 participants