microsoft / microsoft/microsoft-ui-xaml

[WinUI]C++ create and reference a WinUI 3 Component library project failed with MIDL5157 MIDL1001 MIDL2011errors and MIDL2214 warning

Open
#9,889 2 comments 0 reactions 0 assignees View on GitHub
area-ProjectSystem bug team-Core team-Markup
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### 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](https://github.com/user-attachments/files/16595567/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() = default;

void AdjustTemperature(float deltaFahrenheit);
private:
float m_temperatureFahrenheit{ 0.f };
};
}
namespace winrt::ThermometerWRC::factory_implementation
{
struct Thermometer :ThermometerT
{
};
}
```
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;
}
}
```
10. 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)**

1. Open the MyCppDesktopProject solution file in visual studio
2. Right click on the solution node in the solution explorer and select Add->Existing Project
3. Select ThermometerWRC.vcxproj, Note: make sure the version of the Microsoft.Windows.CppWinRT package is updating to a more recent version(>2.0.22*).
4. Right click on the MyCppDesktopProject project node and select Add->Reference
5. Click the checkbox for ThermometerWRC and click OK. Your MyCppDesktopProject.vcxproj should have a reference to the ThermometerWRC.vcxproj file.
6. Right click on the ThermometerWRC project node and select Add-> New Item
7. Select User Control (WinUI 3) and press OK.
8. Build the solution. It won't successfully build but that's fine.
9. 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);
}
```
10. In the pch.h file under the MyCppDesktopProject Node, add the following line:
`#include `
11. Replace the contents of MainWindow.xaml with the following
```



```
12. Delete the myButton_click function from MainWindow.xaml.cpp
13. Build and Run the Solution (F5)

### Expected behavior

Project can run successfully and no error message in Error List

### Screenshots

![image](https://github.com/user-attachments/assets/79ba5d57-2677-4962-b3be-d692a42e2af7)

### 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++)

Contributor guide

Open the contributing guide

Research direction

Download and build the linked MyCppDesktopProject repro with Windows App SDK 1.5.5 and Microsoft.Windows.CppWinRT above 2.0.22. Start with the ThermometerWRC and MyCppDesktopProject project references and the MIDL5157, MIDL1001, MIDL2011, and MIDL2214 output. Done means the solution builds and runs without errors, including the ThermometerWRC UserControl.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
24/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.