3

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

max
  • 1,692
  • 5
  • 28
  • 40

4 Answers4

3

Have a look at
using a class defined in a c++ dll in c# code

Community
  • 1
  • 1
Brij
  • 6,086
  • 9
  • 41
  • 69
2

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
  • 44,254
  • 30
  • 139
  • 205
  • +1. You can actually mix managed and unmanaged C++ in the same DLL. – RickNZ Dec 23 '09 at 06:25
  • @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 Dec 23 '09 at 06:39
  • 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 Dec 23 '09 at 06:42
  • The name of the game is C++/CLI, not CLR. (http://en.wikipedia.org/wiki/C%2B%2B/CLI) – Doc Brown Dec 23 '09 at 06:56
  • 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. – max Dec 23 '09 at 07:03
  • @Doc Brown - Changed to CLI. (Not sure why it's oftern referred to as CLR) – Andrew Shepherd Dec 23 '09 at 08:17
1

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
  • 19,561
  • 2
  • 30
  • 43
0

For anyone who comes to this question and are looking for answers, you may want to try xInterop NGen++ , a C# wrapper generator for native C++ DLL, which has been just released to the public, the tool can generate C# wrapper for native C++ DLL automatically and instantly by using advanced P/Invoke technologies. Check out the current version and a free version will be out soon.

(I am the author of the tool)

xInterop
  • 277
  • 3
  • 9
  • Version 2 appears to still be in early pre-alpha. The UI is cumbersome and no files are generated. ("Exception occurred: Failed to retrieve the PDB information..") – StuWeldon Aug 30 '14 at 04:38
  • @StuWeldon, You probably tried to build a C# wrapper for C style DLL which the current trial version does not support yet, we have added C Style DLL support in the latest version which is not available for download yet. So if you are still interested in using xInterop NGen++ to create wrapper for your C style DLL, please contact us using the contact us page on our web site, we should be able to fix the problem which you were having. Anyone else having any issue, please also contact us at http://www.xinterop.com, we will be in a better position to support you. – xInterop Sep 22 '14 at 01:56