I am interested in using/learning RoR in a project where I have to use a .NET dll. Is Ruby capable of importing a .NET dll?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While IronRuby will make short work of talking to your .NET dll (it’ll be literally no code at all), it was abandoned by microsoft, and it never got a large enough open source community to keep it going after that event. I wouldn’t recommend it these days
Regarding the COM solution, this may actually be a good way to go.
You don’t need the RubyCOM library – that lets other COM objects call into ruby code. To load COM objects from ruby, you just need the
win32olelibrary, which comes as part of the standard library on windows ruby.Whether or not you can load the dll from COM will depend if the .NET dll was built to be ‘Com Visible’. The .NET framework defines a
ComVisibleAttribute, which can be applied to either an entire assembly, or specific classes within an assembly. If it is set to true for either the whole assembly, or any classes, then the dll will already be callable from COM without any wrapper code.Here’s a test I did.
Create a new .NET dll project (class library). Here’s an example class I used:
Now, under the visual studio project, there is a directory called
Propertieswhich containsAssemblyInfo.cs. In this file, there will be the followingChange the false to true. If you don’t want every class in the assembly exposed to COM, then you can leave it set to false in
AssemblyInfo.csand instead put it above each class you want to expose, like this:Now right click on the dll project itself, and from the popup menu, select ‘properties’. In the list of sections, choose
BuildScroll down, and tick the ‘Register for COM interop’ checkbox. Now when you compile this DLL, visual studio will do the neccessary stuff to load the COM information into the registry. Note if you’re on vista you need to run VS as an administrator for this to work.
Now that this is done, recompile your dll, and then create a new ruby file.
In this ruby file, do this:
Where [Solution name] is to be replaced by the name of the solution you just created (default: ‘ClassLibrary1’)
Ruby that ruby file, and presto! you should see that the text gets written to
c:\log.file.One problem with this solution is that it requires that the .NET dll is already Com Visible, or if it’s not, you have the ability to recompile it. If neither of these things are true, then you may have to look at other options.
Good luck!