views:

136

answers:

5

In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected.

But I need it to talk with my C# interface because all other devices are currently using C# interface.

So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code with proper interface) and Call this C++ Library from C# using DLLImport (just call my interfaces there.).

I am pretty sure it can be done this way, but I have never created a C++ Library and used it from C# yet. So, can anyone refer me to some tutorial that goes with my problem?

I am using C++/C# int VS.NET 2008.

Regards, Maksud

A: 

check MSDN here: DLLs

and here: DllImports

Benny
+1  A: 

Depending on the DLL and what you need to do you may not need to create a wrapper directly. You might be able to get away with P/Invoke for the functions. You will need to evaluate your specific needs and what is already available in the libraries/code provided.

GrayWizardx
+1 for the nice article.
Maksud
+2  A: 

Another useful tool you have at your disposal is C++ CLI.

You can use C++ CLI to create an intermediate library - one that exposes managed classes but runs unmanaged C++ code. You can actually mix managed and unmanaged C++ in the same DLL.

The unmanaged portion can accesses the unmanaged DLLs without having to use the PInvoke functions.

Your C# code can access the managed classes in this intermediate library.

Andrew Shepherd
+1. You can actually mix managed and unmanaged C++ in the same DLL.
RickNZ
@RickNZ - That's what I was attempting to convey, but it mustn't have been clear enough. I've incorporated your sentence in the message now, thanks.
Andrew Shepherd
This is definitely the way to go unless you ever plan on porting your library to Mono, which doesn't support native code in C++/CLR.
David Brown
The name of the game is C++/CLI, not CLR. (http://en.wikipedia.org/wiki/C%2B%2B/CLI)
Doc Brown
I was looking at Jonathan Swift's blog (the http://blogs.msdn.com/jonathanswift/archive/2006/10/02/780637.aspx) as the starting point. Now see if CLR solves my problem.
Maksud
@Doc Brown - Changed to CLI. (Not sure why it's oftern referred to as CLR)
Andrew Shepherd
A: 

And yet another article about platform invoke: http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx

Mark Wilkins
+1  A: 

Have a look at
http://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-code

Brij
That's a good example. Thanks.
Maksud