microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 678 forks source link

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

Open AnnYang01 opened 2 months ago

AnnYang01 commented 2 months ago

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 <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns="http://www.w3.org/TR/REC-html40">

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;
    }
}
  1. 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
    1. Select ThermometerWRC.vcxproj, Note: make sure the version of the Microsoft.Windows.CppWinRT package is updating to a more recent version(>2.0.22*).
    2. Right click on the MyCppDesktopProject project node and select Add->Reference
    3. Click the checkbox for ThermometerWRC and click OK. Your MyCppDesktopProject.vcxproj should have a reference to the ThermometerWRC.vcxproj file.
    4. Right click on the ThermometerWRC project node and select Add-> New Item
    5. Select User Control (WinUI 3) and press OK.
    6. Build the solution. It won't successfully build but that's fine.
    7. 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);
      }
  3. In the pch.h file under the MyCppDesktopProject Node, add the following line: #include <winrt/ThermometerWRC.h>
  4. 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>
  5. Delete the myButton_click function from MainWindow.xaml.cpp
  6. Build and Run the Solution (F5)

Expected behavior

Project can run successfully and no error message in Error List

Screenshots

image

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

github-actions[bot] commented 2 months ago

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:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

HO-COOH commented 2 months ago

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.