How to use LuaInterface on Mono/Linux - c#

When I try to use LuaInterface on Mono on Linux (using Mono 2.0 on Ubuntu 9.04) I get the following exception:
** (App.exe:8599): WARNING **: Method ':.DoDllLanguageSupportValidation ()' in assembly
'/home/ulrich/test/Debug/lua51.dll' contains native code that cannot
be executed by Mono on this platform.
The assembly was probably created using C++/CLI.
According to this web site LuaInterface can be used with Mono. MoMA says that too.
Is it possible to recompile lua51.dll to make it compatible to Mono?

LuaInterface looks to be pure C#, but it uses a mixed mode C++/CLI-ified version of the Windows version of the native Lua library, that mixes .NEt code and native 32-bit Windows code. There's no C++/CLI compiler for platforms other than Windows, so you can't port/recompile the C++/CLI code, though it should work on Mono on Win32 (or maybe Wine)..
The only really viable way to get this to work on Mono would be to make it use P/Invokes istead of C++/CLI. You could then use a dllmap so that when Mono tries to resolve the P/Invoke calls to lua51.dll, it is redirected to the Linux equivalent, liblua.so.5.1.

Older versions of LuaInterface use a pure P/Invoke wrapper. You could use this.
There are also a few attempts at alternatives, my own included. http://github.com/jsimmons/LuaSharp

For all of you reading this now: Use KopiLuaInterface!
See my post here: https://stackoverflow.com/a/21386450/1070906

Related

Is it possible to use .NET Native without UWP?

Can a C#/.NET application be compiled to native binaries using .NET Native without being a UWP application? For example, a 4.5 console app? I've watched over 2 hours of video on .NET Native and also read docs but they did not clearly answer this question.
There are not a perfect solution for this but serveral alternatives:
Native AOT, formerly called 'Core RT', which supports full native compilation from managed dlls to binary executables on the target platform(OS and CPU Arch), but it is still marked as 'experimental' (Update: merged into the mainline since .NET 7 preview) with a lot of features missing.
IL2CPP, which is developed and used only by Unity.
CrossGen, which is a part of CoreCLR and could generate .ni.dll files which contains precompiled (native code on specific platform) code rather than IL code in normal managed dll, making it faster loading. But it still requires the runtime because it is basically still a managed dll with JIT compilation already done (AOT).
Note that .NET Framework is going to be obsolete with .NET Core becoming the unified .NET, and you can easily hear from some news about native compilation support if you keep watching .NET Core things

It is possible to use BoxedApp SDK with powershell?

It is possible to load BoxedApp SDK dll with Powershell and use it?
Link to SDK: http://www.boxedapp.com/boxedappsdk/
This is not a managed .NET dll. This is native c++ (I think).
Yes, you can call C++ methods from PowerShell, although not directly. For this you need to use Platform Invoke (P/Invoke). Here are some articles you can start with:
http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/25/use-powershell-to-interact-with-the-windows-api-part-1.aspx
http://msdn.microsoft.com/en-us/library/eyzhw3s8.aspx
Also there are tools which generate C# code, which wraps C++ (like http://www.pinvoker.com/) that you can then use in your PowerShell scripts.
On the theorical point of view, it can be loaded.
PowerShell can use .NET code and .NET can use PINVOKE to load natives DLLs.
But are you sure you need to create such an intricate system ?

Call Mono C# Compiler

For calling MS C# compiler there is CSharpCodeProvider (http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx)
but how do I call the Mono compiler?
I want to know if there was any errors after compilation.
Mono has a better approach available (which should be provided in .NET 5 probably),
http://tirania.org/blog/archive/2008/Sep-10.html
I have not used Mono, but a quick Bing turned up the following Microsoft.CSharp.CSharpCodeProvider in the Mono documentation, is it possible that Mono implements the functionality targeting the Mono C# compiler.
You should take a look at MonoDevelop. It's full featured IDE for the Mono project, speaking of C#, F# and others. You can create a project, generate the classes, paste your source and it'll take care of everything.
Speaking of the compiler:
It's called mcs if you exported mono to your PATH. man mcs/mcs --help.
Probably using Microsoft.CSharp.dll from Mono will call Mono's implementation of Microsoft C# compiler which is the same as Microsoft's.
To call Mono specific features of C# compiler use Mono.CSharp.dll

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.

Using Mono for developing in C++

I am starting to use Mono to develop applications in C# and C++. I wanted to ask you, how is Mono compiling the C++ code? is it using GCC? It is amazing to see that it has the STL containers... Also, can I use the Boost libraries and GSL libraries with Mono? Thanks in advance!!!
I think you must be using MonoDevelop, the IDE, as opposed to Mono itself.
Yes, MonoDevelop uses gcc/g++ to compile C/C++ source code, but it is not compiled to CIL - it is compiled to a native binary.
If I am understanding correctly, then you should be able to use boost just fine.
If, however, you are asking if Mono has support for Mixed-Mode assemblies or executables (e.g. assemblies/exe's that contain both native and .NET CIL), then I am sorry to inform you that this feature is not supported, nor is compiling C++ to pure CIL by Mono.
As long as you don't need mixed mode (i.e., forget the native part and go for CIL-only), mono does work with C++ code (I hear they're now experimentally supporting mixed mode, on Windows especially, and elsewhere via wine, but I think that part's NOT ready for prime time). The one well-supported C++ compiler at this time is Microsoft C++/CLI on Net 2.x frameworks; efforts have been underway (for many years now) to add gcc, but I don't know of any production-ready result so far:-(.

Categories