Sure. https://github.com/Thomas-Tran-UMKC/cpp-cs-interop-issue.git
Notice: I saw something that I didn't notice the first time through. On the C# component, I got an odd warning in the output while building with the types-in-question exposed (public). It still built, but it could be the root of the problem. It's the same error for both methods:
1>CSC : warning CS8785: Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'Win32Exception' with message 'Incorrect function'
followed by: 1>Failed to resolve WinRT.Runtime.dll.
How do I fix the "class not registered" error using a C# WinRT component and Registration-free C++/WinRT console app?

I currently have a working project where I am consuming a C# WinRT component in a win32 console app using registration-free WinRT. Everything is working well, but the problem arises when I try to use Async methods or non-primitive WinRT types like IMap<>
from the C# component. Since Tasks
are not WinRT types, used the AsAsyncOperation()
extension, but this failed too. So what's the correct way to go about calling Async methods and WinRT collection types from a C# WinRT component?
I'm using winmdidl.exe
and midlrt.exe
to create my WinRT comp header file for the console app. I've also tried to include "Winrt/Windows.Foundation.Collections.h" for C++ and the corresponding one for the WinRT comp.
Here is an example that I tried to recreate. Every attempt will build, but always result in the following error at runtime:
Exception thrown at 0x760DB5B2 (KernelBase.dll) in myApp.exe: WinRT originate error - 0x80040154 : 'Class not registered'.
Exception thrown at 0x760DB5B2 in myApp.exe: Microsoft C++ exception: winrt::hresult_class_not_registered at memory location 0x006FEB70.
onecore\com\combase\objact\dllcache.cxx(4815)\combase.dll!7641B4F5: (caller: 76417D37) ReturnHr(1) tid(10c94) 80040154 Class not registered
Exception thrown at 0x760DB5B2 in myApp.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x006FF870.
C# WinRT component:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
namespace MyComp
{
public sealed class MyLib
{
public static string OtherMethods() // only working method
{
return "string";
}
// Below methods results with `Class not registered` error
public static IAsyncOperation<string> TestAsync() // I think this is the problem
{
return TestAsyncHelper().AsAsyncOperation();
}
private static async Task<string> TestAsyncHelper()
{
// do stuff here
return "string";
}
public static IDictionary<int, string> GetMapOfNames()
{
Dictionary<int, string> retval = new Dictionary<int, string>();
retval.Add(1, "one");
retval.Add(2, "two");
retval.Add(3, "three");
retval.Add(42, "forty-two");
retval.Add(100, "one hundred");
return retval;
}
}
}
2 answers
Sort by: Most helpful
-
Thomas T 1 Reputation point
2021-10-25T15:33:06.853+00:00 -
Roy Li - MSFT 33,931 Reputation points Microsoft Vendor
2021-11-09T08:28:53.203+00:00 Hello
@Thomas T Sorry for the delay. I just got some updates about this issue. First of all, IAsyncAction can't be implemented in a C#/WinRT component. This is mentioned in Diagnose C#/WinRT component errors - C#/WinRT components cannot implement certain Windows Runtime interfaces, such as the Windows Runtime interfaces that represent asynchronous actions or operations (IAsyncAction, IAsyncActionWithProgress<TProgress>, IAsyncOperation<TResult>, or IAsyncOperationWithProgress<TResult,TProgress>).. You should use AsyncInfo class for generating async operations in Windows Runtime components.
We are still looking at this issue. Currently, a workaround for your scenario is that you could try to remove those methods' results.
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.