Computing Anything related to trading algorithm, computers, C++, C#, Excel, VBA, Matlab, SAS, S+, R programming, etc

Reply
 
Thread Tools
  #1  
Old 05-18-2009, 10:05 AM
yandong yandong is offline
 
Join Date: Jan 2009
21 Posts, ranked 160
How to combine C++ and C# in Visual Studio?

Hey guys,

Recently I'm working on a project to model some trading strategies. I have already done the computational part in C++, but obviously I don't want the user interface to be only command lines so that I'm trying to build a user-friendly interface in C# and then I could combine these two...

But I really don't know how to do these altho I have googled many thread/websites.. Most say that you could package your C++ code into a dll(I don't know how to do it!) and then I could integrate it into my C# part??

Could anyone give me more specific instructions/advices on how could I achieve so?... Don't know if I have explained my question comprehensively...

Thanks!
Reply With Quote
  #2  
Old 05-18-2009, 11:35 AM
Andy's Avatar
Andy Andy is offline
Quant Network
View Andy's LinkedIn Profile Follow Andy's Twitter
 
Join Date: May 2006
Location: NY
4,409 Posts, ranked 1
Blog Entries: 29
You don't find many google result for this because not many people do want to combine C++/C#
Your choices would be to make wrap the C++ in dll and use Excel as your interface. In all likelihood, Excel UI would suffice for your need. A lot of trading desks this use combination.
The other choice would be to rewrite your C++ computational part into C#. It's trivial and take little time. I've done that and for the most part, it's verbatim. Then use the oodle of visual tool, third party addin, component to let your imagination fly.
Reply With Quote
  #3  
Old 05-18-2009, 03:50 PM
const451 const451 is offline
 
Join Date: Jul 2007
97 Posts, ranked 49
It's easy.

First you have to build your C++ project into a dll (it does not contain main() ). The easiest would be to create a new C++ project as Class Library, copy/paste your code and edit it so you should have only your data and calculations but remove main() and any input/output functions.

Then in the same solution create another project but C# as Windows Forms Application, create the forms and controls where you want to output your data, include you C++ file, create your C++ objects (your data structures are classes?) and consume the data by controls. You have to read msdn about details. But it will work, C# will consume your non-managed C++ data flawlessly.
Reply With Quote
  #4  
Old 05-19-2009, 10:23 AM
yandong yandong is offline
 
Join Date: Jan 2009
21 Posts, ranked 160
Thanks const451..! Now my only question is, how to transform my classes into dll?...



Quote:
Originally Posted by const451 View Post
It's easy.

First you have to build your C++ project into a dll (it does not contain main() ). The easiest would be to create a new C++ project as Class Library, copy/paste your code and edit it so you should have only your data and calculations but remove main() and any input/output functions.

Then in the same solution create another project but C# as Windows Forms Application, create the forms and controls where you want to output your data, include you C++ file, create your C++ objects (your data structures are classes?) and consume the data by controls. You have to read msdn about details. But it will work, C# will consume your non-managed C++ data flawlessly.
Reply With Quote
  #5  
Old 05-19-2009, 10:24 AM
yandong yandong is offline
 
Join Date: Jan 2009
21 Posts, ranked 160
Andy,

Thank you very much for your help! Now I think the only question for me is how to wrap it into dll, which I still don't know yet...

Btw, I m using VS Express 2008.
Reply With Quote
  #6  
Old 05-19-2009, 10:29 AM
Andy's Avatar
Andy Andy is offline
Quant Network
View Andy's LinkedIn Profile Follow Andy's Twitter
 
Join Date: May 2006
Location: NY
4,409 Posts, ranked 1
Blog Entries: 29
You can do it in a variety of way
I wrote something on Quantnet http://www.quantnet.com/forum/showthread.php?t=2509
Use xlw, ExcelDNA, etc
Reply With Quote
  #7  
Old 05-19-2009, 10:03 PM
yandong yandong is offline
 
Join Date: Jan 2009
21 Posts, ranked 160
Quote:
Originally Posted by Andy View Post
You can do it in a variety of way
I wrote something on Quantnet http://www.quantnet.com/forum/showthread.php?t=2509
Use xlw, ExcelDNA, etc
Thanks!
Reply With Quote
  #8  
Old 05-20-2009, 10:25 PM
const451 const451 is offline
 
Join Date: Jul 2007
97 Posts, ranked 49
And if you build your user interface in C# instead of using Excel then you do not need COM Interop. You just reference your C++ classes from C# code - it is much easier. - Just giving you the option.

To pack your C++ classes into dll you create new C++ project (in your existing solution)
as Class Library, so if you build this project it will build into dll instead of exe. Then you just port your classes into this new project.

Reply With Quote
  #9  
Old 06-28-2009, 08:45 PM
zhouxing zhouxing is offline
 
Join Date: May 2009
63 Posts, ranked 65
There are multiple ways to combine C# and C++.

1] Compile C++ code into DLL. Then you invoke C++ code from C# in the same way as invoke DLL function in VB/VBA (check out the import attribute in C#)

2] Compile C++ code into a COM. Then you can use this component as if it is written natively in C# (i.e. reference directly)

3] Hybrid mode

Both of the [1] and [2] are executable level integration. You can also do source code level integration using [3]. It gives your much more flexibility, but at the same a bit more involved too. Google sth like "Managed / Unmanaged Hybrid", C++ Hybrid", you may get some instruction. Alternatively, chapter 4 of this book is dedicated to this topic.
Reply With Quote
  #10  
Old 06-28-2009, 10:33 PM
zhouxing zhouxing is offline
 
Join Date: May 2009
63 Posts, ranked 65
BTW, combining C# and C++ is actually quite useful and very relevant in front office, both in Excel integration and beyond. Many analytic libraries are written in C++. With a C# wrapper, you can no only expose the logic to a nice front end (Excel, WinForm alike), but also instantly add more powers to your C++ code. For example, many "legacy" C++ library are not really multi-threaded, let alone distributed. By adding a C# wrap, you can easily make it "multi-thread", parallel-able, even distributed with much less efforts and risk than to modify the C++ code directly.
Reply With Quote
  #11  
Old 06-29-2009, 12:02 PM
yandong yandong is offline
 
Join Date: Jan 2009
21 Posts, ranked 160
thanks Zhou Xing!

Quote:
Originally Posted by zhouxing View Post
BTW, combining C# and C++ is actually quite useful and very relevant in front office, both in Excel integration and beyond. Many analytic libraries are written in C++. With a C# wrapper, you can no only expose the logic to a nice front end (Excel, WinForm alike), but also instantly add more powers to your C++ code. For example, many "legacy" C++ library are not really multi-threaded, let alone distributed. By adding a C# wrap, you can easily make it "multi-thread", parallel-able, even distributed with much less efforts and risk than to modify the C++ code directly.
Reply With Quote
  #12  
Old 06-30-2009, 12:40 PM
AlexesDad AlexesDad is offline
 
Join Date: Jun 2009
26 Posts, ranked 137
With XLW you can write hybrid C++/C# XLLs. That is, you can write some function in C++ and others in C# all in the same XLL. For your user using your XLL in Excel there is no way of telling in what language the function is written. Its useful for example when you want to write compute extensive functions (like MC pricing) in C++ but want to implement other 'worldly' functions ( e.g retrive market data) in C#
Also you can for example initially implement a function in C# and then if need be re-implement in C++ without affecting the user.
Reply With Quote
  #13  
Old 07-25-2009, 04:09 PM
TrentFGuidry TrentFGuidry is offline
Trent F Guidry
 
Join Date: Jul 2009
3 Posts, ranked 864
There are a few ways to do this.

1) Wrap the C++ code in C style dll's and then use PInvoke to import the dll's. Documentation for doing this on MSDN can be found at http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx.

2) You can wrap the C++ code in COM and then create a managed wrapper around it that can be accessed from C#. I hate that option so much that I won't provide an example of how to do it. I highly recommend against that route unless you absolutely have to go there.

3) Create a Managed C++ project and wrap the C++ code with managed code. I like this option. Documentation for this can be found on MSDN at http://msdn.microsoft.com/en-us/library/b23b94s7(VS.100).aspx.
Reply With Quote
  #14  
Old 08-13-2009, 04:51 PM
Lugh's Avatar
Lugh Lugh is offline
Daniel Duffy
 
Join Date: Jan 2008
Location: Amsterdam
Job: Trainer, Author
47 Posts, ranked 83
here is a post on C# that talks to boost MATHS library (example) using C++/CLI

http://www.datasimfinancial.com/foru...opic.php?t=111
______________________________
www.datasimfinancial.com
Reply With Quote
  #15  
Old 08-23-2009, 11:10 AM
Lugh's Avatar
Lugh Lugh is offline
Daniel Duffy
 
Join Date: Jan 2008
Location: Amsterdam
Job: Trainer, Author
47 Posts, ranked 83
Another scenario ..

Here is now the Excel Addin using C# instead of ATL. It is much easier because of the uniformity of code when compared to ATL code in C++. So we can choose.


We decided to convert this example into an Excel COM add-in. Making it as COM add-in has as advantage that it runs inside Excel and thus can create the Excel sheet much faster as there are no out-of-process calls needed anymore. And the COM add-in still has the advantage that you can use fast C++ code.

Instead of making the COM add-in in C++/ATL, we decided to make the COM add-in in C# using the "Shared Add-in" project wizard. Creating a COM add-in in C++ is difficult and error-prone. Using the C# "Shared Add-in" project wizard is much easier.

The wizard generates a Connect class that implements the IExtensibility2 interface required for COM add-ins. The setup project that is also generated by the wizard, handles registering the add-in in the registry.
In the OnConnection() method of the generated Connect class, we add code to create a menu item that calls our callback function (In .NET this is handled by a delegate). In the OnDisconnection() method we add code that removes the menu item we created in the OnConnection() method. The menu item is in Excel 2007 available in the "Add-In" ribbon menu.



Excel Addin
Reply With Quote
  #16  
Old 12-03-2009, 02:33 PM
DailyVaR's Avatar
DailyVaR DailyVaR is offline
 
Join Date: Nov 2007
8 Posts, ranked 335
Quote:
Originally Posted by TrentFGuidry View Post
There are a few ways to do this.

1) Wrap the C++ code in C style dll's and then use PInvoke to import the dll's. Documentation for doing this on MSDN can be found at http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx.

2) You can wrap the C++ code in COM and then create a managed wrapper around it that can be accessed from C#. I hate that option so much that I won't provide an example of how to do it. I highly recommend against that route unless you absolutely have to go there.

3) Create a Managed C++ project and wrap the C++ code with managed code. I like this option. Documentation for this can be found on MSDN at http://msdn.microsoft.com/en-us/library/b23b94s7(VS.100).aspx.

I've looked into this same exact problem a few months ago. These suggestions are good ideas.

Personally, I would avoid COM because I think it ancient technology that should not be perpetuated in 2009. PInvoke is kind of kludgy but its incredibly easy and fast to test, assuming your code is already a C++ DLL. You should be able to call PInvoke with a few lines of code in C#. .NET does a lot of the conversions for you.

Managed wrappers are a pain as you will need to jacket every unmanaged call in your library with a managed one. If its just function calls, C-style, this is no big deal. If its code with classes that return back objects that need to be passed in as parameters to other method calls, you will need to rearchitect your API. Also, PInvoke is slooooow. If you are calling this function inside a Monte-Carlo simulation for each scenario you are going to get killed.

There is also some interfaces developed by MS. There are two Apress books: one on C++/CLI and the other on .NET 3.5. I forget which book has which but one of them covers the older interface and the other covers the new interface. I would go to your local Barnes and Nobles and browse both books.
______________________________
Check out my blog, rndness.com, on quantitative finance and computer technology.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create UDF in Excel using C# and Visual Studio 2008 Andy Computing 39 01-06-2010 10:58 AM
MS Visual Studio 2008 SekharNY General 14 08-23-2008 11:59 AM
Free Visual Studio 8 Professional Edition DominiConnor Computing 1 03-13-2008 06:50 PM
Visual Studio and Other Microsoft software available from CIS club at Baruch Phat Loc Baruch College 0 12-04-2006 03:01 PM


All times are GMT -4. The time now is 06:13 AM.