question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET MAUI Throws Exception w/ Lib C#/WinRT

See original GitHub issue

Description

TL;DR Project to reproduce: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui A .NET 6 console app Main.Console works fine w/ the C#/WinRT lib Lib.CsWinRT. However, when comes to the .NET 6 MAUI app Main.MAUI, it throws an exception:

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Lib.CppWinRT.MyStruct..ctor()
   at Main.MAUI.MainPage.OnCounterClicked(Object sender, EventArgs e) in C:\Users\...\source\repos\microsoft\winrt\starter\src\Main.MAUI\MainPage.xaml.cs:line 16
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(Object sender, RoutedEventArgs e)
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
COMException: Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))

PS: I follow the walkthrough guide of C#/WinRT: https://docs.microsoft.com/en-us/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component PPS: Remember to build at first the project Lib.CsWinRT to generate the NuGet package.

Any ideas?

Steps to Reproduce

  1. Clone my sample project: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui
  2. Launch Main.MAUI (on Windows Machine) and click the button

PS:

  • Main.UWP works => Lib.CppWinRT works
  • Main.Console works => Lib.CsWinRT works

Version with bug

Preview 10 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows SDK 10.0.19041

Did you find any workaround?

Nope

Relevant log output

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at WinRT.WeakLazy`1.get_Value()
   at WinRT.ActivationFactory`1.ActivateInstance[I]()
   at Lib.CppWinRT.MyStruct..ctor()
   at Main.MAUI.MainPage.OnCounterClicked(Object sender, EventArgs e) in C:\Users\...\source\repos\microsoft\winrt\starter\src\Main.MAUI\MainPage.xaml.cs:line 16
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(Object sender, RoutedEventArgs e)
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
COMException: Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jingkecncommented, Mar 22, 2023

Hello @jfversluis, sorry for late reply. I just tried the latest versions of VS + MAUI Workload, the issue seems to be fixed. You can refer to the repo I mentioned before, I just updated it: https://github.com/jingkecn/Microsoft.WinRT.Starter/tree/app/main-maui

0reactions
msftbot[bot]commented, Mar 22, 2023

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET MAUI Throws Exception w/ Lib C#/WinRT
Console works fine w/ the C#/WinRT lib Lib.CsWinRT. However, when comes to the .NET 6 MAUI app Main.MAUI, it throws an exception: System....
Read more >
My maui app throws exception if I open second window ...
Exception thrown : 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.dll WinRT information: The WinUI Desktop Window object ...
Read more >
Updated to Telerik.UI.for.Maui v6.0 and get errors when ...
I updated the version of the Telerik controls from 5.0.1 to 6.0.0 and some popups have started to throw errors.
Read more >
How to call WinRT from MAUI code-behind in the right ...
Throws exception : "The application called an interface ... CoreLib.dll ... This exact code works fine in a regular non-MAUI UWP/C# project.
Read more >
Adding a System.Guid to ApplicationDataCompositeValue ...
Here is the native exception: Exception thrown at 0x00007FFE63734B59 (KernelBase.dll) in StockDesktopWidget.Startup.Win.Packaging.exe: WinRT originate error - ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found