2

This is kind of a complicated situation. I'm refactoring (ground-up) c++ that must be used as both a CGI script and the core of a standalone app.

Unfortunately I haven't written C++ since college, and am more familiar with c#/Java. So I'm going to use WPF for the GUI. From what my research has revealed, this means that I will be:

1) Refactoring the base code in unmanaged C++ (yay for intellisense support, eh?)

2) Wrapping that base in a managed class library

3) Wrapping step 2 in a C#/WPF GUI, referencing it through interop (CLI/CLR).

step 2, I am confident, will be well-documented. However, I'm starting at the top and can't find anything concrete on how to instantiate and use a class created/exposed in managed c++.

I'd just write the whole thing in c# if I had my druthers (no need for hardcore efficiency), but I don't have that choice.

Can someone point me in the right direction for step 3 so that I can start researching it? I don't want anyone to do my homework for me (so to speak), but my own research into this process has been fruitless.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Eric_H
  • 35
  • 7
  • Note: Managed C++ no longer exists. C++/CLI is it's replacement. While the former is an extension of C++ , the latter is a completely new language. Use of "Managed C++" will probably lead to confusion because people will think you're talking about Visual Studio 2003 – Billy ONeal Jun 15 '11 at 18:42

1 Answers1

3

If you wrap your unmanaged C++ classes in managed CLI / C++ objects, then you don't need Interop to invoke the managed classes (Visual C++ will compile both managed and unmanaged C++ in the same library).

Simply reference the mixed managed / unmanaged library to your managed C# application and the exposed managed objects will behave like any normal object.

For a good starting point check out this article.

Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
  • So I don't have to use any kind of special magic to convert data types / built-in types when moving between c# and managed c++? That's a bit of a relief, I'll be honest. – Eric_H Jun 15 '11 at 18:34
  • @Eric I mean, it depends on how complicated the public interfaces to your CLI / C++ classes are. It can be as simple or as complicated as you make it. – Brandon Moretz Jun 15 '11 at 18:38
  • Since the eventual deployment environment is CGI (hence the double-wrapping) I'm going to be making it as simple as possible. Thanks again for the info. – Eric_H Jun 15 '11 at 18:39
  • I may be in a similar situation; does anyone know how to automatically generate the C++/CLI wrapper classes? In a large product, maintaining these by hand will be a huge, boring, error-prone nightmare. – James Johnston Jun 15 '11 at 20:09
  • @James Johnston SWIG has the ability to generate wrapper code for C++ classes. It generates both some C++ interop code and the associated C# wrapper code. It takes some effort to get working - you should check it out. – mcdave Jun 16 '11 at 02:44
  • Yeah, I ended up going with SWIG, it's a bit of a bear to get working, but once you've got it down, it really helps in a situation like mine (multiple deployment environments with differing compilers). – Eric_H Jun 17 '11 at 19:56