开发者

Creating C++ Dll, and call it from C#

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开发者_JS百科 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


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


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜