Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a C# dll in ruby?

How to call a C# dll in ruby?

like image 623
zhessy Avatar asked Dec 01 '09 03:12

zhessy


People also ask

How do you call C in C++?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.

How do you call C from R?

The most basic method for calling C code from R is to use the . C() function described in the System and foreign language interfaces section of the Writing R Extensions manual. Other methods exist including the . Call() and .


4 Answers

I can think of a few possibilities:

  • Write (or get someone to write) a COM wrapper for your DLL, if it doesn't already have one, then use Ruby's WIN32OLE library to call it;
  • Take a look at RubyCLR, one of the authors of which was John Lam, who went on to work on IronRuby at Microsoft. (I don't think it's maintained any longer, and it may not support .Net versions above 2.0);
  • As already mentioned elsewhere, look at using IronRuby, if that's a technical option for you.

There's a thread on the topic here. Note that the last post is actually from John Lam (looks like March 2009) where he seems comfortable asserting that RubyCLR is still functional...

like image 156
Mike Woodhouse Avatar answered Oct 18 '22 20:10

Mike Woodhouse


You can also write a native -> C# wrapper DLL using managed C++

Export all the functions you want as C calls in the DLL, e.g.

extern "C" __declspec ( dllexport ) void CallManagedMethod() {
   Something^ myManagedObject ...
}

Then use FFI to call that DLL from Ruby https://github.com/ffi/ffi

like image 28
Rich Avatar answered Oct 18 '22 19:10

Rich


You can use unmanaged exports (https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports) to create an unmanaged entry in c#. For example you can created an init_youmodulename, which is required for a ruby extension. Then you can use require 'youmodulename' to load it in ruby.

This link (https://bitbucket.org/icehuli/sucsexttutorial) has several examples. Although it aims in ruby extensions for Sketchup, it may still be interesting for you to learn how it works.

like image 36
ice.huli Avatar answered Oct 18 '22 20:10

ice.huli


With IronRuby http://ironruby.net/

like image 33
BJ Clark Avatar answered Oct 18 '22 19:10

BJ Clark