tags:

views:

159

answers:

2

Let's say I have written a Class Library (dll) in .Net and now I have developers using it in their .Net applications.

However, the library itself could probably be useful also for developers writing natively (in C or C++). So my question is if my managed dll can be used in C or C++?

If not, why? Maybe I must add some specific code to make it available to native coders?

Thanks.

EDIT: In case anyone else is interested in this issue, I found this article from Google Books which gives an introduction how to use Net.classes from COM.

+9  A: 

The only way to expose a .NET assembly outside of a CLR-based language is through COM Interop. There is nothing that you can do to reference a .NET .dll directly in an unmanaged language.

The reason for this is that the ".dll" file extension used is purely for appearances and the illusion of consistency. .NET generated .dll files do not contain any machine code, they contain IL (intermediate language) that is compiled at runtime (called Just-In-Time compilation, or JIT). The code is not compiled at the time that it is placed in the .dll file. As a result, there is nothing for the unmanaged language to execute.

COM interop allows the CLR to load the .dll, perform that JIT compilation, and use the COM system to handle communication between the native code and the .NET .dll.

Adam Robinson
I have no objections! My knowledge of this is not much and I am learning. Many thanks for your explanation. However, if a C++ developer wants to call my managed dll/assembly and use its methods and so on, do you confirm me that he can do so as long as he uses COM Interop (which I don't know anything about) or must I do something with my library to enable COM Interop for him? Sorry for all questions but I am rather new to this advanced stuff.
moster67
You have to mark you assembly in .NET as being COM exposed (this is a project property), and you have to do some plumbing work with the code in order to make it work. There are many articles out there on how to expose your .NET assembly and classes to COM. The C++ developer will then have to use the COM interface in order to interact with your classes. It is by no means a simple or seamless process.
Adam Robinson
Adam: many thanks for the hints. I guess I have to collaborate with someone developing in an unmanaged language and together try to get this to work. Once again my thanks for your help. Have a good day/weekend.
moster67
+1  A: 

I have to disagree with Adam Robinson. His description, especially the comment explaining the COM-exposed property is certainly one way to implement it. Thoe other is letting the C or C++ application host the Microsoft CLR and have that run your .Net DLL. You might be able to host Mono, too, it should be equally capable of running pure .Net DLLs. (i.e. no P/Invoke tricks)

MSalters
@MSalters: While being able to host the CLR in your application is a great way to make your application extensible (i.e., by allowing developers to write plugins in a .NET interface), it's not a real (or viable) solution for simply "using" a .NET dll in your application. The C++ plumbing for this is fairly extensive, and it's hardly intuitive. I do suppose it's a theoretical possibility for accomplishing the goal of using a .NET assembly in native code, but the COM solution seems more appropriate and is a low effort compared to this.
Adam Robinson
Oh, I'd definitely agree with you if you had started with "By far the easiest way to ...". This post was just trying to complete the list of options. I mean, hosting Mono appears even scarier. Yet that might be the only way if you want to use this .Net DLL in a native Linux C app
MSalters