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/
Related
We have a requirement to import a C++ legacy dll inside C# UWP application and access the methods inside the c++ classes. We don't have the source code with us, So not able to do with the Windows Run time component.
Please let me know how can I import the c++ dll inside the Visual C# universal windows application.
I am able to add the dll as a reference in visual C++ application, But not able to do it in visual C# uwp application. I have tried the dllimport but it is throwing dll not found exception.
Firstly, UWP can't import a legacy C++ dll just by DLLImport.
If you want to import legacy c++ functions to C#, the first suggestion is to wrap that C++ logic using a WinRT component.
And if you want to PInvoke the dll, you can follow these steps (You can refer to this thread):β First, add dll into your UWP project making sure to set its type as 'content'β. Then in the proper cs file, using DllImport to PInvoke the dll.β
In addition, you need to make sure your dll is not using prohibited APIs in WinRT. You can check this by using /ZW compile option for the dll. There is a similar thread, you can refer to it.
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.
I am currently working on a c++ application built with /MT switch and would like to port my application to C# Desktop App. I also plan to use WinRT APIs in the c# desktop app.
Using WinRT APIs in c++ requires the project to be compiled with /MD switch instead of /MT, and i was wondering what kind of problems would I encounter when porting the app to c#.
All the libraries linked in the c++ app are also compiled with the /MT switch.
From what i could find on the internet, its possible to use libraries compiled with either of the above two mentioned switch(using c++/CLI wrapper or P/Invoke).
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
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.