Serving the Quantitative Finance Community

 
User avatar
quanteric
Topic Author
Posts: 6
Joined: June 4th, 2010, 12:01 am

Interfacing C++ and C#

January 11th, 2012, 6:46 am

Hi everyone, I am wondering if anyone can help me with interfacing... I have built a C++ DLL and am wondering how I can integrate that with C# code. There may be an additional complication in that some functions exposed in the DLL have user-defined types as inputs and outputs. I would very much appreciate your help on this.ThanksPS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks.
 
User avatar
ludinski
Posts: 0
Joined: January 9th, 2009, 8:54 am

Interfacing C++ and C#

January 11th, 2012, 7:21 am

You will have to use the Microsoft specific C++ language extensions in order to expose your C++ code to the .net runtime as common language runtime class library. Basically using Visual C++ create a new project: New->Project->Visual C++->CLR->Class Library.Here's an example:http://msdn.microsoft.com/en-us/library ... .100).aspx
Last edited by ludinski on January 10th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Govert
Posts: 0
Joined: January 28th, 2006, 10:02 am

Interfacing C++ and C#

January 11th, 2012, 10:13 am

If you are able to expose or wrap your C++ API as a C interface (using your own struct / data types is no problem), then interop via P/Invoke is very easy, fast and reliable. And there are lots of resources on the web.If you have a very rich C++ interface, some wrapper using the Microsoft C++/CLI managed extensions is a way to make managed classes in C++ that easily talks to your native C++ code.A third option, more adventurous since the project has just started up, is to try Xamarin's new CXXI bridge. Xamarin are the Mono (cross-platform .NET) guys and make tools to let you develop Apple and Android apps in C#.CXXI uses the GCC compiler to generate binding info that is compiled to a .NET assembly that allows you to seamlessly interop with your C++ code. While it is still early days - for example I'm not sure of the current state of the MSVC binding - I think it's a good project to watch. My guess is that this will be the best C++/C# interop story in a few months. If you have some patience, I'm sure they'd love the feedback.-Govert Excel-DNA - Free and easy .NET for Excel
Last edited by Govert on January 10th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
ludinski
Posts: 0
Joined: January 9th, 2009, 8:54 am

Interfacing C++ and C#

January 11th, 2012, 11:36 am

QuoteOriginally posted by: GovertIf you are able to expose or wrap your C++ API as a C interface (using your own struct / data types is no problem), then interop via P/Invoke is very easy, fast and reliable. And there are lots of resources on the web.I ve never tried it but yet another option would be to use swig (simple wrapper interface generator) in order to generate the corresponding P/Invoke code: swigI have used swig in order to interface c++ with python, ruby & java and it works flawlessly. I'm sure the c# interface is just as robust.In particular have a look at the swig java documentation, section 21.2.8 in order to setup your Visual Studio environment for swig: swig java
Last edited by ludinski on January 10th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Interfacing C++ and C#

January 11th, 2012, 7:03 pm

QuotePS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks. "C# in a Nutshell" v 4.0 is excellent.
 
User avatar
tags
Posts: 3632
Joined: February 21st, 2010, 12:58 pm

Interfacing C++ and C#

January 11th, 2012, 8:29 pm

QuoteOriginally posted by: CuchulainnQuotePS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks. "C# in a Nutshell" v 4.0 is excellent.Illustrated C# 2010 explains everything very clearly. various passages of it can be read via the books of google.Apress editions has other good titles for the one who wants to develop .NET skills. The "Books" section of datasimfinancial.com reads a book on C# is due soon.
Last edited by tags on January 11th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Jew
Posts: 0
Joined: November 22nd, 2011, 6:46 am

Interfacing C++ and C#

January 12th, 2012, 10:16 am

QuoteOriginally posted by: quantericHi everyone, I am wondering if anyone can help me with interfacing... I have built a C++ DLL and am wondering how I can integrate that with C# code. There may be an additional complication in that some functions exposed in the DLL have user-defined types as inputs and outputs. I would very much appreciate your help on this.ThanksPS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks.Greetings, From .Net platform point of view there are managed, unmanaged and native codes. All that works upon entire .Net platform is managed code, native code is processor instruction set and the rest is unmanaged code. So if you want to programmatically interop between C# and managed C++, you can simply use System.Reflection namespace. So if you want to programmatically interop between C# and unmanaged C++, you should use "Platform Invoke" operations, for example, after declaring unmanaged part of code you need to build a wrapper in your C# class.For example:C - code (I don't know C++ syntax ):BOOL WINAPI DllMain(/*you need to read that how to build Dll*/){//...}extern int __cdecl GetZero(){return 0;}C# - code:using System.Runtime.InteropServices;namespace WrapperCode{public static class Demo{static string yourpath; //...[DllImport(yourpath, /*calling convention = cdecl (as in unmanaged code), etc. optional parameters*/)] public static int GetZero();//...}//console entrypoint}That's all. If you want to work with non-static objects etc. you need to learn more interoperability tricks (mdsn.com).
Last edited by Jew on January 12th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
farmer
Posts: 63
Joined: December 16th, 2002, 7:09 am

Interfacing C++ and C#

January 12th, 2012, 12:53 pm

I cannot think of any reason to ever use C#. If you want to make a form that stores fields in a database as fast as possible, use LAMP. If you want to use an extra C function, either a) add a socket to the function, put it in an executable, and send the data over a PHP socket, or B) make the function an extension of PHP by putting it in a .so file.
Antonin Scalia Library http://antoninscalia.com
 
User avatar
quanteric
Topic Author
Posts: 6
Joined: June 4th, 2010, 12:01 am

Interfacing C++ and C#

January 18th, 2012, 12:30 am

Thanks guys. I am very grateful of your help. However, please do forgive me for being ignorant on C# and interfacing and allow me to ask further questions. Can you tell me what is the distinction between managed and unmanaged C++ code? Is there a beginner's guide to C#/C++ interfacing floating somewhere on the web that I can possibly take a look at?Thank you again for your help on this, very much obliged.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Interfacing C++ and C#

January 18th, 2012, 6:22 am

Last edited by Cuchulainn on January 17th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Rufus
Posts: 4
Joined: January 18th, 2002, 5:24 pm

Interfacing C++ and C#

January 18th, 2012, 9:04 am

QuoteOriginally posted by: quantericHi everyone, I am wondering if anyone can help me with interfacing... I have built a C++ DLL and am wondering how I can integrate that with C# code. There may be an additional complication in that some functions exposed in the DLL have user-defined types as inputs and outputs. I would very much appreciate your help on this.ThanksPS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks.The Jon Skeet book C# in depth and the follow up Real World Functional Programming are both good. Nicely written and they dig into the interesting stuff of C# (and F# in the second book).
 
User avatar
Jew
Posts: 0
Joined: November 22nd, 2011, 6:46 am

Interfacing C++ and C#

January 18th, 2012, 11:02 am

QuoteOriginally posted by: quantericThanks guys. I am very grateful of your help. However, please do forgive me for being ignorant on C# and interfacing and allow me to ask further questions. Can you tell me what is the distinction between managed and unmanaged C++ code? Is there a beginner's guide to C#/C++ interfacing floating somewhere on the web that I can possibly take a look at?Thank you again for your help on this, very much obliged.Greetings,It's simple idea, that managed code is the code that executes on virtual machine side. See articles on the "Wikipedia" about virtual machine, intermediate language, .Net etc. So if you write the code upon .Net framework, your C++ code is managed because this code should be executed on the .Net side. And if you don't write your C++ code via .Net framework, your code will work as unmanaged: without any resources, memory etc. managing by .Net platform. .Net is similar to Java platform but .Net is with resources managing and etc. very useful (for some purposes) "black magic" (see articles about TPL, for example).
Last edited by Jew on January 17th, 2012, 11:00 pm, edited 1 time in total.
 
User avatar
Jew
Posts: 0
Joined: November 22nd, 2011, 6:46 am

Interfacing C++ and C#

January 19th, 2012, 10:26 am

QuoteOriginally posted by: quantericHi everyone, I am wondering if anyone can help me with interfacing... I have built a C++ DLL and am wondering how I can integrate that with C# code. There may be an additional complication in that some functions exposed in the DLL have user-defined types as inputs and outputs. I would very much appreciate your help on this.ThanksPS Could anyone recommend a good book on C# for someone who is fairly proficient in C++? Thanks.http://www.codeproject.com/KB/cs/unmanage.aspxHere is example for you. And here is some additional topic for you:http://stackoverflow.com/questions/2636 ... in-c-sharp
Last edited by Jew on January 18th, 2012, 11:00 pm, edited 1 time in total.