views:

65

answers:

3

My .net project needs to use a third party class library written in C++. I have already tried directly adding a reference to this DLL from the project's context menu 'Add a refence...'' but it did not work as I assume this DLL does not implement the COM object model.

What options do I have now? Some suggest to create a .net wrapper around this DLL but I have never done it in the past so I'm looking for an easy introduction with possibly some examples. The closest thing I have done in the past was calling methods from an external library using pInvoke (from win32.dll for instance) but in this case I might also need to wrap native C++ types to their equivalent representation in .net and there might also be some instance variables to be accounted for so I don't know if the pInvoke will be sufficient.

I'd really appreciate if anyone could point me in the right direction and please let me know if more information is required.

Many thanks.

+1  A: 

There are 2 options that I know of

  1. P/Invoke which is the DllImport route
  2. C++/CLI wrapper (here is a tutorial)
PieterG
+1  A: 

Assuming you don't have the source to the library or there is another reason why you can't transform the library into a C++/CLI assembly.

You might be interested in SWIG ( http://www.swig.org/ ) a wrapper generator. You can give SWIG the public interface to a C++ library and it knits a wrapper for any of its supported backend languages, which C# happens to be one of.

Luther Blissett
Seems nice tool.
user001
+1  A: 

As you said there are 2 ways to do that:

1) By importing C++ functions using P/Invoke.

2) By wrapping unmanaged code to mananged and add that library in your .net application using 'Add Reference'. Here is the simple example for this.

This link will give you detailed information of .net interoperability.

Out of these P/Invoke is easiest way to do. For c++ equivalent types in .net, you can do marshaling which is also easy and you will find lot many tutorial for the same.

user001