-
Notifications
You must be signed in to change notification settings - Fork 122
Description
Description
I'm trying the "hello world" scenario of consuming a C++/WinRT Runtime Component using a C# console application.
The compilation succeeds and the projection types get generated with no problems, but whenever I run the application, I get the following runtime exception:
System.Runtime.InteropServices.COMException: 'Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))'
I searched online and some references were saying it might be something wrong/incompatible with platforms. That is why I tried a whole bunch of different project configurations, and the only change that worked for me is when I added <PlatformTarget>x86</PlatformTarget> to my C# project. Changing to <PlatformTarget>x64</PlatformTarget> and running the program as x64 didn't work; only x86 works.
I'm new to the whole WinRT and COM world so I have no clue what is going wrong, and I need to be able to run the application on different platforms. Help is really much appreciated!
Version Info
Visual Studio: 17.9.1
Windows SDK: 10.0.22621.0
CppWinRT NuGet: 2.0.220531.1
CsWinRT NuGet: 2.0.7
.NET: 6.0, 7.0, and 8.0 (i.e., I tried on all versions)
Context
Here is how I created the projects...
- Created a project using "Windows Runtime Component (C++/WinRT)" template
- Implemented the
runtimeclassas follows:
// Class.idl
namespace RuntimeComponent1
{
[default_interface]
runtimeclass Class
{
Class();
Int32 MyProperty;
}
}// Class.h
#pragma once
#include "Class.g.h"
namespace winrt::RuntimeComponent1::implementation
{
struct Class : ClassT<Class>
{
Class() = default;
int32_t MyProperty();
void MyProperty(int32_t value);
private:
int32_t m_data = 0;
};
}
namespace winrt::RuntimeComponent1::factory_implementation
{
struct Class : ClassT<Class, implementation::Class>
{
};
}// Class.cpp
#include "pch.h"
#include "Class.h"
#include "Class.g.cpp"
namespace winrt::RuntimeComponent1::implementation
{
int32_t Class::MyProperty()
{
return m_data;
}
void Class::MyProperty(int32_t data)
{
m_data = data;
}
}- Created a C# Console Application
- Added
CsWinRTNuGet - Added reference to the C++/WinRT Windows Runtime Component project
- Updated the
csprojto add the following property group:
<PropertyGroup>
<CsWinRTIncludes>RuntimeComponent1</CsWinRTIncludes>
<CsWinRTGeneratedFilesDir>$(OutDir)</CsWinRTGeneratedFilesDir>
</PropertyGroup>- Updated
Program.csas follows:
using System;
RuntimeComponent1.Class c1 = new ();
c1.MyProperty = 5;
Console.WriteLine(c1.MyProperty);