1

As it is described here I'd like to use a DLL in my C program. The first two options I am considering are:

1. Host CLR

My problem here would be the note from this article:

Hosting methods provided by versions of the .NET Framework before the .NET Framework 4 are deprecated. We recommend that you use the interfaces introduced by the .NET Framework 4 and discussed in this topic.

In my case the DLL files are working under the .NET Framework 2.0 so this could be a problem in the first place.

  • Does anybody know if I can do this anyway somehow?

2. Register for COM interop

The other option is using COM. In this case I would need to know what exactly does the option Register for COM interop in Visual Studio 2010. The DLL file is part of a library that our company also gives to customers. So what I need to know is:

  • Which additional information are stored inside the DLL?
  • Could there be a reason why a company would not want to register its DLLs for COM interop?

Thanks for any information!

Community
  • 1
  • 1
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

2 Answers2

3

Using Regasm.exe (aka "Register for COM interop") does two things:

  • it records where the DLL is located so it can be automatically found. You just use a number in your own code, a GUID.
  • it records which CLR version is required to run the code. You automatically get the right one.

So it solves the exact problem you are trying to solve :) No coincidence of course. Hosting the CLR yourself has the advantage that you can call any .NET code, not just the kind that's [ComVisible]. It isn't otherwise a way to avoid having to learn how to write COM code, the hosting interfaces and the primary .NET interfaces you need to call .NET code (like _Assembly) are also COM based. Using C instead of C++ is cruel and unusual punishment, best avoided.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • So would you recommend using this option? I still have not really an idea of CLR. All I know is that I have large C# Class Libraries which I have to use in a C program - unfortunately I can not chose other languages here since both project are conciderably large.. Is there an example about how to do that somewhere which I could take a look at? – Stefan Falk Feb 03 '14 at 15:10
  • There are books about it, Steven Pratschner's is quite good. The odds that you'll ever find example C code are rather low. Cruel and unusual low. – Hans Passant Feb 03 '14 at 15:20
  • Would trying to port the C project into Managed C++ be an option? I have no idea if that would work but since C is a subset of C++ this should be doable? – Stefan Falk Feb 03 '14 at 15:24
  • 1
    It is not entirely impossible but "considerably large" are ominous words. It certainly doesn't magically turn C code into managed code. – Hans Passant Feb 03 '14 at 15:51
  • Well yes I saw that clr only understands objects .. so C code would not be compileable. I am already searching for an alternative solution - maybe sending commands over network - just to avoid messing around with this issue .. – Stefan Falk Feb 03 '14 at 16:07
1

To answer your question: 1. You definitely can host a 2.0 in your process. Jeffrey Richter recommends using CorBindToRuntimeEx Function in the 2.0 edition of his CLR via C#. This got deprecated in the latest versions of the framework, hence the confusion. I would recommend going this way.

2. COM interop - to make it work properly you actually need to change your library, add some attributes, then register the resulting COM object, etc. I am pretty sure it adds references to some marshallers and other COM related stuff. I would definitely stay as far as possible from COM. The shortcomings of this technology was one of the reasons to come up with .Net in the first place. And even more so if you ship these dll's to anyone.

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • Thank you! This is good to know and thanks for resolving the confusion on point 1! I think I'll go for the CLR solution. – Stefan Falk Feb 03 '14 at 14:16