8

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?

miradham
  • 2,285
  • 16
  • 26
  • 1
    Expose your class library as COM component and then use it as COM component from native code. – Ayub Jul 24 '18 at 06:47

2 Answers2

2

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.

Barr J
  • 10,636
  • 1
  • 28
  • 46
1

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://learn.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.

ADG
  • 343
  • 1
  • 9
  • Isn't C++/WinRT to use existing winrt APIs? Is there any way to use custom winrt/wrl libraries with C++/WinRT? – miradham Jul 30 '18 at 06:51
  • According to MS C++/WinRT is intended to replace C++/CX and can be used to both build and consume WinRT apis. That would include custom APIS. FYI I have used C++/CX to build and consume custom WinRT APIs but not used C++/WinRT. The purpose of both technologies is a the same though, hide the ugliness of COM. – ADG Jul 30 '18 at 11:09
  • well building WinRT apis is one deal and calling them from Native Win32 C++ is another. Your answer looks promising though. Would appreciate if you could provide real example – miradham Jul 30 '18 at 11:24
  • All my real world code is C++/CX. Microsoft examples are at https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/consume-apis. Full documentation is available at the link I already provided. There is also a lot of material on YouTube. – ADG Jul 30 '18 at 11:53