Question

I have an unmanaged class that I'm trying to dllexport from a managed DLL file. I'm trying to use the unmanaged class in another managed DLL file. However, when I try to do this I get link errors.

I've done this lots of times with unmanaged DLL files, so I know how that works. I know how to use "public ref", etc. in managed classes.

Is there some flag somewhere I need to set? Or do I have to do some DllImport magic?

This is on .NET 2.0 and Visual Studio 2005.

Was it helpful?

Solution

You need to use an interop assembly for unmanaged libraries or COM components. Here is a link with good information regarding this.

OTHER TIPS

If you want to use an unmanaged class from managed code, you can:

  1. Use some nasty P/Invoke to load the class member functions (constructor, destructor, etc.) and make a nastier managed wrapper class to call the "real" member functions. This gets even worse when there are virtual methods involved.
  2. The second option (which in my opinion is better) is to write a C++/CLI wrapper class (you mentioned that you're familiar with this) that is a simple proxy to the unmanaged class. For every member function you have in the unmanaged class you'll have a similar one in your proxy class. Then it's a simple case of adding a reference to that DLL from your managed project, and you'll be able to use your class as any other .NET class. Take into consideration that you'll run into more work if your class exposes other unmanaged stuff (those that can't be marshalled).

If you need more information on the second option, I could look up some old links that explained this technique more.

If it is a COM DLL file then you can use COM .NET interoperability. Stack Overflow question Is there a best practice for accessing C++ native COM functions to interop from C#? is another question which answers this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top