using c++ libraries built with /MT /MD switch from c# - c#

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

Related

Use of cross-platform C++ dynamic library from C# Uno project

I'm working on the port of the C# UWP project to Uno-Platform. The original project references a lot of logic from C++ DLLs. The interface between C# and C++ library is rather big to write PInvoke wrappers manually.
Is there any complete solution to automate the calling of C++ library methods from .NET?
The Uno Platform does not provide anything specific for this type of scenarios, and relies on what .NET already provides (P/Invoke in this case).
You may want to take a look at https://github.com/EgorBo/CppPinvokeGenerator or https://github.com/xoofx/CppAst.NET to generate C# from the C++ code.

How to import C++ Legacy DLL in C# UWP application without Windows Runtime Component

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.

using c++ wrapper in c# .NET project

I'm using the C++ wrapper in this link:
https://github.com/TekRTSA/RSA_API
In order to use a driver written in C++ inside C# project.
When I publish/release my C# project it doesn't work on other computers without Visual C++.
Is there any way that I can publish my project on computers that don't have the visual C++? Or is there any software package like .NET that I can use in order to make it work?
With .NET applications you need to make sure customers have a version of .NET that matches what you compiled your application with.
With (Microsoft) C++ applications that depend on the C Runtime, you need to make sure customers have a version of the Visual C++ Redistributables that matches what you compiled your application with. Do a google search for 'visual c++ redistributable' and you will see lots of suggestions on what to look for.

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/

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