Can a standlone Mono-based DLL be called from Ruby? - c#

I have a C# class library that contains some specialized mathematical functionality relating to raster processing (it uses some asynchronous techniques to spped up the processing). I have a project that my client wants written using Ruby on Rails (RoR) on a Linux box, that can benefit from using this library (I'd rather not port it to Ruby or C++).
The first part of my question is:
Can a standalone Mono-based DLL be created from my C# DLL? (By standalone I mean, the DLL can be utilized WITHOUT having mono installed (all required libraries would be included in the single DLL)?
And the second part, if the answer to part 1 is 'yes', can this standalone mono-based DLL be called from Ruby?
EDIT 11/22/2014
I have made a little progress possibly. I have created a *.so file by using
mono --aot -O=all dlltest.dll
(Obviously I now have Mono installed on my Ubuntu test VM.) This results in dlltest.so, which I then add a symbolic link for in /usr/lib.
The code for my test Ruby script is:
require 'ffi'
module CsharpTest
extend FFI::Library
ffi_lib 'dlltest.so'
attach_function :Hello, [], :string
end
ret_str = CsharpTest.Hello()
puts ret_str
Note that my simple C# class is:
using Systems;
namespace DllTest
{
public static class MyClass
{
public static string Hello()
{
return "Hello World";
}
}
}
When I run the test Ruby script (ruby test.rb) I get the following error:
....:in 'attach_function': Function 'Hello' not found in [dlltest.so] (FFI::NotFoundError)
Using 'nm -Ca dlltest.so' the available functions are listed. One of them is '0000000000001010 t DllTest_MyClass_Hello'. If I use this label as opposed to 'Hello' I get a similar error.
Also note that 'nm -D --defined-only dlltest.so' does not list any functions with 'Hello' in them.
I'm still not sure if I've on the right track, but is there anything more I can try? Or is this approach a dead end?

Related

How to organize C# code in different files without GUI?

I'm beginner to C# and have to develop some C# code under linux.
Until now, I could work it all out in a single file, just by using the two commands mcs and mono, without any GUI. And if possible, I'd like to avoid the GUI.
But now I want to seperate code in different files or even folders, but I could not find any explanation online.
Let's say I have some code like this
public class SpecialVector
{
// some code
}
in some special.cs file. How can I use SpecialVector in some other file, let's say normal.cs? (Btw how do you call code grouped in a file in C#?)
Excuse me if this question has already been asked, but I don't know how to twist it so that the search engine understands my problem...
As has been mentioned in the comments, if you aren't limited to Mono for a particular reason, you are highly encouraged to switch to .NET Core (now called .NET as of version 5). It has native support for both Windows, Mac and Linux, and comes with command line tools that allow you to create and build entire multi-file projects in a single command, without having to specify each individual file manually.
That being said, if you continue using Mono, you can specify multiple files to the compiler in the following manner:
mcs File1.cs File2.cs
(and so on)
Have a look at the man page for mcs for more info.

Can PHP Extensions be written using C#

Is it possible to create a PHP Extension using C#?
We have a need to connect PHP to in house libraries we have written in C#.NET 4.6 targeting Windows and would like to be as close as possible to PHP without needing to have a serivce we can call external to php, thus the idea of making an extension in house.
I've looked at a couple options:
Use PHP DOTNET extension to call C# assembly.
As far as I can tell this extension does not work with .NET 4+ and the C# code requires 4.6 sadly.
Write a PHP extension.
So far I have only seen examples for Windows using Win32 and C++, idealy it would be nice if the extension could be written in C#. I have thoughts on making a lib using the C# code we need, then utilizing that inside the Win32/C++ extension and trying that if I absolutly have to.
Call the C# code as an external service.
As a very last option this would be viable and I can see this being more flexible for other sources to use (i.e. PHP, Java, C#, etc)
It would be interesting to be able to write a PHP Extension using C# assuming this is possible. I can see how this may not be the "best" option if it is possible though.
Thanks for reading and advice!!!
Coming off of #SevaAlekseyev idea I was able to get PHP to call C# using COM.
<?php
$object = new COM('ComClassExample.ComClassExample');
echo '1 and 1 added: ' . $object->AddTheseUp(1,1);
?>
Using the following site I was able to make a C# dll that exposed itself like a COM object. https://whoisburiedhere.wordpress.com/2011/07/12/creating-a-com-object-from-scratch-with-c/
And with a little tweak to the Post-build event I was able to get the new version to load for PHP (had to copy because PHP was getting an access denied message)
iisreset /stop
copy /Y "$(TargetPath)" C:\com\$(TargetFileName)
$(frameworkdir)\$(frameworkversion)\regasm.exe C:\com\$(TargetFileName) /codebase /tlb /nologo
iisreset /start
#JuanDelaCruz Peachpie looks like an interesting tool to keep in mind but it looks like it is meant to be compiled before deployment which wont work in our shop for the moment.
Thanks all!

Matlab output to C#

I have a complicated Matlab function which I would not like to re-write in C#. The function returns an array of N double-precision numbers.
Given that I have compiled the function to into a .NET assembly (a .dll file), and that the function's signature goes like [resutls] = myFunc('stringInput'), how can I call my function inside a C# code?
Thanks!
Here you can find the steps to do that:
https://www.mathworks.com/help/compiler_sdk/gs/create-a-cc-application-with-matlab-code-1.html
Is necessary have the runtime library installed on the computer that run your code (you can add it when create the .dll package)
The way I did it, is by adding MLApp as a project reference.
From MATLAB, you need to start the automation service:
enableservice('AutomationServer', true);
And within C# you can connect to Matlab using.
MLApp.DIMLApp matlabInstance = (MLApp.DIMLApp)Marshal.GetActiveObject("Matlab.Desktop.Application");
You can then use the interface functions of MLApp to interact. E.g.
int a = (int)matlabInstance.GetVariable("variableName", "base");
Or even execute stuff. E.g.:
matlabInstance.Execute("evalin( 'base' , 'plot( range , dataVector , ''k'');' );");
of course you need some error handling, etc. Normal application stuff.
There's a topic about it here

Load DLL file and Call Functions in Python

I have the problem loading the DLL file and calling the functions in Python.
I have tried a lot of tutorials, but still can't figure out how it works.
This is my class to export as DLL file. I use simple C# code.
namespace DemoClassLib
{
public class cLib
{
public int increment(int x)
{
return x + 1;
}
}
}
After building the C# code, I get the DLL file called "DemoClassLib.dll".
I use ctypes to load the DLL file. Everything is okay until now.
from ctypes import *
myDll = WinDLL('D:\\Resources\\DemoClassLib\\bin\\Debug\\DemoClassLib.dll')
Starting from that point, I can't continue.
All the commands I have tried are failed.
n = c_int(1)
myDll.increment(n)
It keeps on showing me the errors.
How can I call the method "increment()" in Python?
And how can I pass the input and retrieve the output from that?
I am very new to Python.
Can someone help me please?
I would be very appreciated if you can provide me the source code tutorial.
You can't do this with ctypes because there is no symbol in the binary called simply "increment", as your increment method is a member of a class. Even if this were C++ the name would be mangled. But with C# you don't even get a mangled name in the symbol table because the code is interpreted by the .NET framework.
If you must for some reason interface with a C# library you may want to consider trying IronPython (http://ironpython.net/) which is Python running on the .NET framework with full access to the CLR. The comment above suggesting exposing a COM interface could also work.

how to use piping in C#

I have a exe in c++ where i have created CreateIPCQueue function and i am creating another exe in c# where i want to use this method.then how i should proceed for this.pls help me
Powershell (.NET) and Piping
You might be able to make use of Windows Powershell to pipe between classic program output and cmdlets built in .NET. Because it pipes objects instead of raw text between programs, I'm unsure it's compatibility with piping classic strings, although you might find a unique solution here so I'll post it now and update later if a more specific variation is found...
Update:
In Powershell, I tested and found that classic text output can be piped into .NET cmdlets. For example, I took the standard text output from the C# compiler help screen and piped it into the ForEach-Object construct - in this case each object is a .NET String because the classic output is represented textually.
# In Powerhsell.exe #
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /? | ForEach-Object -Process {"LINE: " + $_}
It produced the following output that proves the concept - indeed it prepended the phrase "LINE: " to each line of text.
LINE: - ERRORS AND WARNINGS -
LINE: /warnaserror[+|-] Report all warnings as errors
LINE: /warnaserror[+|-]:<warn list> Report specific warnings as errors
LINE: /warn:<n> Set warning level (0-4) (Short form: /w)
LINE: /nowarn:<warn list> Disable specific warning messages
LINE:
LINE: - LANGUAGE -
LINE: /checked[+|-] Generate overflow checks
Any other .exe that produces text output can be used in this example.
Next Steps
Based on that proof of concept showing classic text and .NET objects interoperating through Powershell piping, the next step might be expose your .NET program as a cmdlet (to substitute in place of the For-Each construct in the example). I looked for a useful article about exposing your existing .NET code as a cmdlet and found: Creating a Windows PowerShell CmdLet using the Visual Studio Windows PowerShell Templates
Basically you can expose a piece of your existing .NET logic as a cmdlet and then rely on the natural piping ability inherent in Powershell. The bonus is you'll be compatible with Powershell and other programs can reap the benefits of using your cmdlet.
Update 2
Additional Resources
Extend Windows PowerShell With Custom Commands
Cmdlet Development Guidelines
Writing a Windows PowerShell Cmdlet
I think what you are taking about is that you want to use the UNMANGED c++ funciton from
MANAGED C# environment. That means you want to use interop.
If this is correct, then you can follow the below:
Suppose in CPP(UNMANAGED) if have the following function(A basic Calculator):
//User Program Starts from Here
extern "C"
{
__declspec(dllexport) int MyCalculator(int _FirstNo,int _SecNo,char _op)
{
switch(_op)
{
case '+': return(_FirstNo + _SecNo);break;
}
}
}
Next , I want to call this function from my MANAGED C# Environemnt.
The below will help
public partial class Form1 : Form
{
[DllImport(#"F:\CppDll.dll")] //is the path where the CPP dll is located
private static extern int MyCalculator(int _num1, int _num2,char _operator);
int _Result = 0;
public Form1()
{
InitializeComponent();
}
private void btnSum_Click(object sender, EventArgs e)
{
_Result = MyCalculator(20,10,'+');
Messagebox.Show( _Result.ToString()); // Output: 30
}
}
Hope this helps
NET provides the System.IO.Pipes namespace for working with pipes since 2.0. It supports named and anonymous pipes, access and auditing rules.

Categories

Resources