views:

462

answers:

4

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

I've done this lots of times with unmanaged DLLs 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, VS2005.

+1  A: 

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

hmcclungiii
So it looks like I have to use PInvoke from that link. What a pain. I can't just use dllimport, like with a normal unmanaged DLL.
Nick
A: 

Is it a COM dll? If yes, then you can use COM .net Interoperabality. Here is another question which answers this.

Vinay
It's a managed DLL (as stated), not a COM DLL.
Nick
A: 

this help you : interop

lsalamon
This doesn't link to anything.
Nick
All results are tagged by interop, and are about your question.
lsalamon
+2  A: 

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 (ctor, dtor, 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 info on the second option, I could look up some old links that explained this technique more.

Idan K
Thanks for the feedback. I'm trying to avoid writing a wrapper because I want to read a large amount of data in small chunks very quickly and if I stay unmanaged I know what the memory overhead is. But don't want to use PInvoke either! :(
Nick
if that's the case then you might want to consider a different design. maybe some kind of buffering. or perhaps move some logic from your managed code to unmanaged to reduce the calls to unmanaged code.
Idan K