microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
[WinUI]C++ create and reference a WinUI 3 Component library project failed with MIDL5157 MIDL1001 MIDL2011errors and MIDL2214 warning
Describe the bug
C++ Create and reference a WinUI 3 Component library failed with the below errors, please download the repro project and build this project. MyCppDesktopProject (2).zip
| Severity | Code | Description | Project |
|---|---|---|---|
| Error | MIDL5157 | [msg]An invalid winmd file was specified for import [context]: D:\Users\v-liliyatang\source\repos\MyCppDesktop\x64\Debug\ThermometerWRC\ThermometerWRC.winmd | MyCppDesktop |
| Error | MIDL1001 | [msg]cannot open input file [context]Error opening or processing WinMD d:\users\v-liliyatang\source\repos\mycppdesktop\x64\debug\thermometerwrc\thermometerwrc.winmd. HRESULT 0x80070002. | MyCppDesktop |
| Warning | MIDL2214 | [msg]semantic check incomplete due to previous errors | MyCppDesktop |
| Error | MIDL2011 | [msg]unresolved type declaration [context]: Microsoft.UI.Xaml.Controls.UserControl [ RuntimeClass 'ThermometerWRC.UserControl' ] | ThermometerWRC |
Steps to reproduce the bug
Step1:Create a functional Windows Runtime component (WinUI 3 Component Library) 1.Create a Windows Runtime Component (C++/WinRT) project, and name it ThermometerWRC (for "thermometer Windows Runtime component"). Make sure that Place solution and project in the same directory is unchecked. 2.Don't build the project yet, and make sure the version of the Microsoft.Windows.CppWinRT package is updating to a more recent version(>2.0.22*). 3.In Solution Explorer, rename "Class.idl" file to "Thermometer.idl". 4.Replace the contents of "Thermometer.idl" with the listing below:
// Thermometer.idl
namespace ThermometerWRC
{
runtimeclass Thermometer
{
Thermometer();
void AdjustTemperature(Single deltaFahrenheit);
};
}
5.Save the file. Build the project. 6.Right-click the project node and click "Open Folder in File Explorer". 7.Copy the stub files "Thermometer.h" and "Thermometer.cpp" from the folder \ThermometerWRC\ThermometerWRC\Generated Files\sources\ and paste into the folder that contains your project files, which is \ThermometerWRC\ThermometerWRC.(Replace the existing files) 8.Open "Thermometer.h" in VS, replace the content with below code:
// Thermometer.h
#pragma once
#include "Thermometer.g.h"
namespace winrt::ThermometerWRC::implementation
{
struct Thermometer : ThermometerT<Thermometer>
{
Thermometer() = default;
void AdjustTemperature(float deltaFahrenheit);
private:
float m_temperatureFahrenheit{ 0.f };
};
}
namespace winrt::ThermometerWRC::factory_implementation
{
struct Thermometer :ThermometerT<Thermometer,implementation::Thermometer>
{
};
}
9.Open "Thermometer.cpp" in VS, replace the content with below code:
// Thermometer.cpp
#include "pch.h"
#include "Thermometer.h"
#include "Thermometer.g.cpp"
namespace winrt::ThermometerWRC::implementation
{
void Thermometer::AdjustTemperature(float deltaFahrenheit)
{
m_temperatureFahrenheit += deltaFahrenheit;
}
}
- Save and build the project.
Step2: Create new C++ Desktop Project
Create a C++ Blank App, Packaged (WinUI3 in Desktop) and name the project MyCppDesktopProject.
Step3: Create a functional Windows Runtime component (WinUI 3 Component Library)
- Open the MyCppDesktopProject solution file in visual studio
- Right click on the solution node in the solution explorer and select Add->Existing Project
- Select ThermometerWRC.vcxproj, Note: make sure the version of the Microsoft.Windows.CppWinRT package is updating to a more recent version(>2.0.22*).
- Right click on the MyCppDesktopProject project node and select Add->Reference
- Click the checkbox for ThermometerWRC and click OK. Your MyCppDesktopProject.vcxproj should have a reference to the ThermometerWRC.vcxproj file.
- Right click on the ThermometerWRC project node and select Add-> New Item
- Select User Control (WinUI 3) and press OK.
- Build the solution. It won't successfully build but that's fine.
- Replace the existing my_ButtonClick method in UserControl.xaml.cpp with the following
void UserControl::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
ThermometerWRC::Thermometer t;
t.AdjustTemperature(20);
}
- In the pch.h file under the MyCppDesktopProject Node, add the following line:
#include <winrt/ThermometerWRC.h> - Replace the contents of MainWindow.xaml with the following
<Window
x:Class="MyCppDesktopProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyCppDesktopProject"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:control="using:ThermometerWRC">
<Grid>
<control:UserControl/>
</Grid>
</Window>
- Delete the myButton_click function from MainWindow.xaml.cpp
- Build and Run the Solution (F5)
Expected behavior
Project can run successfully and no error message in Error List
Screenshots
NuGet package version
WinUI 3 - Windows App SDK 1.5.5: 1.5.240627000
Windows version
Windows 11 (22H2): Build 22621, Windows 10 (21H2): Build 19044
Additional context
It didn't repro on rel. d17.12-35209.166 (WindowsAppSDK version: 1.5.240311000 for C++)
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!
Open similar issues:
- CS0246 and MSB3073 error occur when C# Project Referencing a WinUI 3 Component library (#8455), similarity score: 0.82
Closed similar issues:
- Build break when C++ Create and reference a WinUI 3 Component library using WinAppSDK 1.4.230913002 (#8925), similarity score: 0.82
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Do not even try to rename an idl file after it is created. All the internal file references will go wrong. Rename the idl when you create it, not after.