How to access mono native code using php - c#

Ahead of Time Compilation or AOT is a feature of the Mono runtime code generator.
mono --aot program.exe
This will generate a file called "program.exe.so"
How can i load this shared object file in php script and access the class objects and methods. ?
Thanks

The native library still needs to be loaded inside an AppDomain (i.e. the Mono VM/runtime) in order to run, it is not a native library as such.
If you must I'd suggest looking at
whether php supports COM interop (I don't use php, but I'd reckon the chance exists). This would be good since you could use that and profit from OO interface exposure
Use Swig which has support for C# some time now
Alternatively, use mkbundle, and/or create a native shared library that embeds a Mono VM. The shared library wrrap around the C# interface using a "C" native API's.

The Phalanger project should be able to do this. You can compile your php code with mono and also integrate with .net from php.

Related

Using .NET CLR in luajit as a module

I'm writing a C/C++ application which uses a luajit runtime. In this runtime I want to load .NET DLLs and call functions/create objects exposed by it.
After a lot of research I've found some libraries which can achieve this, luainterface for example. But the problem with all these libraries is that they require a Lua runtime running in .NET itself.
Is there a library which I can preferably require as a module from which I can use .NET DLLs?

COM Interop in Mono 2.0

I am trying to use this code in a Unity project, but it seems the implementations of COM Interop in Mono/.NET differs, which causes the code to fail or crash. Running the code in .NET works fine, but running it with Mono 2.0 (outside of Unity) fails in the same way as in Unity, suggesting it is a problem with Mono in general and not Unity.
If I compile and run the code as-is, it fails because the type cast from MMDeviceEnumerator to IIMMDeviceEnumerator fails. When decorating all interfaces with [ComInterop], the cast succeeds, but the call to GetDefaultAudioEndpoint crashes Unity/Mono with an Access Violation.
It is hard to find good documentation of COM Interop on Mono in general - and particularly so regarding such an old version. Is it at all possible to get this running?
Wrap the COM functions in C funtions and call the C functions via P/Invoke instead. This can be done in two steps:
Create a VC++ project that wraps the functions you needed in wasapi. Expose them via a module define file or __declspec(dllexport). Build the code into a dll that exposes the functions you need.
In your Unity3D project, access them via P/Invoke.
Here is an example. In your case, just use the COM code in the C/C++ part to do what you want.
Mono 1.0 and Mono 1.1.xx do not have support for COM.
Stop trying with Mono, Mono is for platform independence and COM Interop is Microsoft only. Use open source SDKs for Video Playing or better invoke apps from command line like vlc to play, encode etc.

Writing C# managed code in native C++

I am developing a managed lib (using Microsoft Web Services) and I am
including it into a c++ project. The project doesn't use /clr option,
so when I include my library's header file VS2005 show me an error
saying I have to use /clr option. Doing this I have a incompatibility
with /EHs command line option (error D8016), but changing from EHs to
no exception handling not solving problem and keep showing me same error .
Any suggestion is welcome.
Thank you in advance.
If you have unmanaged C++ code and want to use managed code, you have a few options:
Change your unmanaged code to C++/CLI, by use of the /clr switch.
Write a C++/CLI wrapper library. It could DLL-export unmanaged functions which you call in your unmanaged code.
Skip the wrapper library and directly DLL-export unmanaged functions via this library.
You can't use a managed lib from an unmanaged c++ application. Since you add the /clr option, your c++ application becomes managed too (just for the record :) )
Here's what might help you: http://msdn.microsoft.com/en-us/library/ffkc918h.aspx - the restrictions of the /clr option.
It is possible to write managed c++ adapter, that will call the C# library, and call this adapter from unmanaged c++ program as you would usually call a normal c++ library. You will compile your adapter library with /clr and your main c++ program without /clr if for whatever reason you want to keep it unmanaged.
You can embed a mono environment and start an AppDomain. mono's runtime API will allow you to instantiate classes and call members on them. It will be clumsy, but is will work
http://www.mono-project.com/Embedding_Mono
Note that Mono is a full .Net 4.0 compliant CLR and it can work with the Microsoft core libraries on Windows.
On windows and Unix it can work with the Mono corlib/class libraries. There are areas not covered in Mono, but they seem to get sparse. You can use the MoMa tool to spot whether your application uses incompatible/incomplete APIs.
Or you can just use the Microsoft .NET framework, assuming you're on windows anyway!

Calling a MATLAB function from C#

I developed a MATLAB function, and I'm looking for a way to call that function from another C# application and pass some parameters to it and get the results in the C# program.
I heard that I can use Dynamic Data Exchange (DDE) or COM objects, but have can I do it?
There is nice example in the MATLAB Central.
It shows three ways on how to communicate with MATLAB:
COM
MATLAB .NET Bulider
MATLAB compiler
COM (I do not have any experience with it)
Cons: MATLAB is required to be installed on the target computer.
MATLAB .NET builder compiles your MATLAB code to the .NET assembly and you can use it directly.
Pros: MATLAB is not required to be installed on the target computer
Cons: It's expensive
MATLAB compiler compiles your MATLAB code into a C/C++ library or EXE file. You can use it through P/Invoke.
Pros: MATLAB is not required to be installed on the target computer
Cons: It's expensive, a lot of P/Invoke.
There is a third option: delegates. Starting MATLAB -> load .NET assembly -> execute .NET function with delegate handle to a MATLAB function.
There is a great example on this site on setting up everything. You can use MATLAB .NET deployment tool.
You need to
Install MCR (Matlab Compiler Runtime).
Deploy your Matlab function to .NET Assembly using Matlab Deploy
Tool. This will create a .dll file.
Add .dll reference inside your .NET project.
Add reference to MATLAB.NET.
The advantage of this method is that the target machine does not require MATLAB to be installed, but on the downside the execution is quite expensive.

Auto wrap c++ dll into c#

I want to use c++ library in a c# project. Is there any wrapper tool to import all classes automatically?
SWIG can help create a wrapper consisting of two parts, one C++ sided, and one C# sided.
It needs a bit of work to set up the correct generation files though.
An alternative that requires more manual coding is C++/CLI.
For pure c apis I prefer p/invoke over either of them. There is a program to automate conversion of c headers. If I recall correctly it's called something like "P/Invoke Interop Assistant" or "Interop Signature Toolkit".
There is also mono/cxxi which looks pretty cool.
The procedure of using native .dll's in .Net is called P/Invoke. Look at http://pinvoke.net/ for some examples.
Note that you must match the build target with the version of the .dll. So for x86 .dll's you need to lock your project to x86, same with x64.
Note2 that you only need to lock the executing project (.EXE), not any additional projects loaded from the .EXE. .Net will automatically match .Net .dll's to CPU target type if they are set to ANY.
From http://social.msdn.microsoft.com/forums/en-US/clr/thread/c957959e-0f0c-422e-a5be-4ccfdd12e63d: You can use "dumpbin /exports <name_of_your_dll>" or dependency walker (depends.exe) to look at the exported symbols. They are both included in Visual Studio.
Additional comment on C++: While it is relatively simple to use native .dll's written in C from .Net, using C++ binaries that make use of objects is not as trivial. One way to solve that is to use a C++ CLI project a binding between managed .Net code and unmanaged C++ library.
If this is unmanaged code then you could use P/Invoke. Another possibility is to use the C++/CLI extensions to compile the code into a managed assembly that you could directly use.

Categories

Resources