Use Universal Windows C# class library from Native C++ - 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?

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

Related

Calling UWP C# code from UWP c++/cx

I have an UWP C++/cx application and UWP C# class library.
How can i use classes from c# library in c++ project?
C++/CLI not supported by UWP apps so i cant make c++/cli wrapper.
Im not sure but looks like COM wrapping isnt an option in UWP world.
Reverse P/Invoke is not an option as host application is c++
Windows Runtime Component also will require kind of callback from C# code,
but it can be instantiated in c++ code only.
Any suggestions?
P.S.
I cannot have "c# proxy app", my app is of type c++/cx.
Standart uwp c# classlibrary is invisible for c++/cx.
Proxy library "windows runtime component" should be created and added to c++ class.
For a great example of how to do this, check out how ChakraBridge C++/CX project in React Native Windows is used:
https://github.com/Microsoft/react-native-windows/tree/master/ReactWindows/ChakraBridge
C# calls into C++/CX and then back out, totally transparently, living up to the cross-language workflow the CLR allows. There's also a pull request in React Native Windows that shows how to add a few compile-time directives that allows for sharing C++/CX and C++/CLI code:
https://github.com/Microsoft/react-native-windows/pull/769/

What is the right pattern for accessing a C# DLL from C++?

I generally work on Android/Java, but right now I'm working on Windows Phone 8.1. I have C++ code in a library that needs to be able to access the functionality in a provided C# Assembly DLL.
The C# DLL has an interfaces that needs to be implemented to receive callbacks from the DLL.
What I've tried:
I tried using a Windows Runtime Component to wrap around the DLL , but then it complains that the interface isn't valid to use with Windows Runtime Components.
I can wrap the DLL in a pile of C# code, and provide an interface in that to forward on any callbacks from the DLL to any implementation (C++), but the C++ code doesn't want to recognize the namespace as an existing namespace.
What is the missing piece?
I was able to access the DLL using a C# Windows Runtime Component. The callback interfaces from the DLL were implemented in C# and would call back into the C# Windows Runtime Component to do work.
The work to be done was calling a C++ Windows Runtime Component that would do the talking to the C# Windows Runtime Component. The C++ Windows Runtime Component could then be the gateway to the C++ library of code we have.
The C++/C# interoperability can be achieved in two ways: through Win32 DLLs or through COM components.
If you plan to use the Win32 approach you're gonna have to provide all the metadata in terms of CLR types, then the managed compiler will compile the metadata into the assembly and the JIT compiler will build stubs that performs the C#/Win32 conversion.
If you plan to use the COM components you'll have to use the Runtime Callable Wrappers, that will take care of instantiation, type navigation, error handling and memory management.
More information about RCW is here: http://msdn.microsoft.com/en-us/library/8bwh56xe(v=vs.110).aspx

Using C# dll in C++ code

I need to integrate this C# dll
in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.
There are basically two cases to call a .NET DLL from unmanaged code:
The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):
2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.
I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.
PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

How can I make a Win32 DLL that is reference-able in a C# application?

I have a rather large legacy nmake (Win32) project that creates a static library from native C++ code. I need to use this library in a C#/.Net application. In the past after much effort I had been successful at wrapping the static library in a managed C++ library, which I am then able to reference in a C#/.Net application. However, after receiving updates from the developers of the nmake project, and having gone through an many upgrades on my own build machine in the meantime, it is no longer working.
I am however able to import the cpp and header files of the nmake project and build it to a Win32 static library in VS 2010, by setting all of the preprocessor constants in the build properties. I set the build configuration type to DLL, and then try to add a reference to the Win32 output in my C#/.Net application hoping to use P/Invoke down the road, and it fails with a message "A reference to MyLibrary could not be added."
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
If you want to directly reference the library, you'll need to build a C++/CLI project using your library, and make managed wrappers.
If you want to use P/Invoke (which is a separate concept), you can make exports with a C API, and call those directly via P/Invoke.
The two approaches are both valid, but completely different in terms of implementation (C++/CLI vs. C API wrappers) on the native side, as well as used differently (directly referenced for C++/CLI vs. P/Invoke via [DllImport]).
You can use SWIG to generate wrappers for your code. SWIG is a very powerful tool and worth taking the time learn. It creates wrappers for a number of languages including Python, C#, and Java so if you get it working with one language it is fairly easy to use in other languages as well. It will generate all the wrapper code for you, although you will probably need to do some work with type. You use swig to create a special DLL that SWIG generates code for and then used supplied C# code to access the DLL without needing to deal with managed C++ assemblies which can be a nightmare to deal with.
http://www.swig.org/Doc2.0/SWIGDocumentation.html
Edit: my explanation may not be that clear and the docs are pretty overwhelming, take a look at the "What is swig?" section here to get started:
http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_nn2

Calling C# from native C++, without /clr or COM?

I have a class library written in C#, and I want to call it from a legacy native C++ application. The host application is truly native, compiled on Windows and Linux, and it’s a console application. So how can I make it call the C# class library, assuming using Microsoft .NET on Windows, and Mono on Linux?
I have looked at SWIG and wrapping with COM interfaces on Windows, but is there a standard recognized solution that works cross platform? I.e., that is generic, works with both Microsoft .NET and Mono. A write-once-use-everywhere implementation.
Solutions should expose the full class interfaces from the C# domain to the C++ domain.
Similar questions focus only on the Windows solutions, for example -
Call C# methods from C++ without using COM
If you want to do this cross platform, I would recommend going with a 100% Mono approach.
Mono has a clean Embedding API which works on Linux and Windows.
With .NET 5.0 (the successor of .NET Core) this is now possible to call C# from C++ in a cross-platform way without using Mono. Please see the solution explained in this GitHub issue using DNNE to generate a shared library and GCHandles to access C# objects.
With this you get a shared library that can be used from C or C++. Note that this will give a C-like API (no objects, like when using extern C in C++), in the future there may be tools like SWIG for C++ to overcome this limitation.

Categories