Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Universal Windows C# class library from Native C++

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?

like image 770
miradham Avatar asked Jul 20 '18 04:07

miradham


People also ask

What is universal C?

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.

Can you use C++ in UWP?

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.

Does Windows 11 use UWP?

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

What is WinRT used for?

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.


2 Answers

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.

like image 157
Barr J Avatar answered Nov 14 '22 22:11

Barr J


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.

like image 44
ADG Avatar answered Nov 15 '22 00:11

ADG