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.
Related
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.
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 have a dll created in c# which does some database operation upon calling. I have to pass few parameters from a Java application to a method in the dll. Any Idea on how to do this.
OR
Any better ideas for calling a method in c# by a java application?
Thanks
Edit : My java application is a standard Dialogue Designer application from AVAYA Platform and I have developed a C# application which works on a SDK for .net only. So i need data to be passed from java application to .net application
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability
The third point is maybe what you are looking for.
Project Site here: http://www.ikvm.net/
I looked into this a while back although I enver got far with it as the requirements changed. But I did read that using JNI, Java Native Interface may be able to help you with what you are needing to do.
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
Hope this helps!
I have a dll developed in C Sharp. How can I make it usable in other languages, such as PHP or Delphi?
I cannot find any solution to this problem yet. Isn't there any easy way of doing this?
If Delphi can understand COM, then you can make a COM wrapper around the .NET DLL.
I am doing this now for an older IDE which doesn't understand .NET, but it understands COM so I am able to use the latest .NET features in my older applications.
In addition to COM, you could try these (from my Delphi Win32 to WCF answer) for Delphi Win32:
Use Delphi Prism with the UnmanagedExport attribute to create a .NET wrapper that you can call from native Delphi Win32
Use Managed VCL to do .NET interop from native Delphi Win32
From another Stackoverflow question: create a C++ DLL crossing the bridge this used "mixed mode C++"
Use RemObjects Hydra on both the C# or Delphi Prism and native Delphi Win32 side (RemObjects wrote Delphi Prism, so this works like a charm)
The last one might be the real one you are after.
For PHP it is a different story;
PHP might run on a non-Windows box,
there are .NET implementations of PHP that would allow for direct linking of your C# assembly DLL
--jeroen