I have Universal Windows C# class library with UI components. I was wondering if I can use it from Native C++.
I tried to use regasm to convert class library dll into tlb file, but it throws error
Error: Assembly must not be a Windows Runtime assembly.
Also I tried to make a WinRT/WRL wrapper for C# class library, and tried to load it from Native C++. But when I call LoadLibrary
for wrapper dll, it returns 'nullptr' with 126 error, even though all dlls and executables are in the same directory.
So how can I use Universal Windows class library from Native C++? Is it possible?
The Windows 10 Universal CRT is a Windows operating system component that enables CRT functionality on the Windows operating system. This update allows Windows desktop applications that depend on the Windows 10 Universal CRT release to run on earlier Windows operating systems.
The Universal Windows Platform (UWP) is the modern programming interface for Windows. With UWP you write an application or component once and deploy it on any Windows 10 or later device. You can write a component in C++ and applications written in any other UWP-compatible language can use it.
UWP is one choice for creating apps that run on Windows 10 and Windows 11 devices, and can be combined with other platforms. UWP apps can make use of Win32 APIs and . NET classes (see API Sets for UWP apps, Dlls for UWP apps, and . NET for UWP apps).
C++/WinRT is an entirely standard modern C++17 language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API.
You will have to expose your class library as COM component and call it from native code, this is the most convenient solution.
you won't be able to call Universal Windows C# class library from native C++, as it won't be recognized and, as you mentioned in your question, it will cause a nullptr
exception.
The interesting things is, you can do the other way around!
you can create a native C++ library and call it in Universal Windows C# platform - there is whole post in MSDN regarding this practice:
Use Existing C++ Code in a Universal Windows Platform App
funny thing in my opinion you can do one thing but not the other way around but still, it's good to know that at least one way is actually possible.
I am not sure "convenient" is the word I would use to describe exposing native C++ as a COM component.
You should take a look at C++/WinRT
https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis.
It appears to supersede C++/CX which was Microsoft's initial approach to allow C++ to be used to build and use WinRT components.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With