views:

79

answers:

3

Hi SO,

for several reasons I need to port a C/C++ unmanaged project (VS 2008) to C# (preferably .net 3.5).

I'd need to know:

  • whether, by any chance, exist some conversion-helping tools; let's say something translating the code syntax and asking you verifications/modifications for each problematic point (I guess I'm dreaming...)
  • where to find some useful howtos/articles... about it. They would be very useful if they contained specific hints like:

    extern variables should be set in public static classes (I don't know, I'm guessing...)

Please no suggestion like "You can call your c++ dll from .net", because I know it's possible, but just I can't.


P.S.

I've searched on goole and SO before asking, but I could't find any link/question/answer similar or helpful enough. If you find one or you think this question has to be closed, please don't hesitate

P.P.S.

Sorry for my poor english.


EDIT:

Some additional infos:

  • The C/C++ project uses only STL and other basic functions, no 3rd Party libraries etc.
  • I can't uses it direclty or wrapped from C# because our company need to mantain/modify the code and we're extremely more skilled in c# than in c++.
+1  A: 

Depends on what you mean by port.

You can rewrite some of the stuff in C#. Not everything. Some HW or legacy libraries would have to be handled with C/C++ even if you port your own code. I don't know of any reliable automatic converter for C++->C#, and I doubt one can exist.

A better idea may be to wrap your existing code in new C# code. You could create an interop layer in C++/CLI, for example. Or you can communicate with your native code with something like Google Protocol Buffers, if you don't want to mix native/managed code in the same process.

Assaf Lavie
I'd really really prefer not to wrap it. Check my edits ;)
digEmAll
+1  A: 

I doubt code conversion tools would help. If you need to make some C++ work in some fashion with .NET, the easiest way is to write a managed C++ layer that wraps it and provides an interface for .NET apps to work with. But it depends on the code.

What is the purpose of rewriting and what does your code do? Does it interface with other components? Does it have a GUI? Is it a standalone executable or a library? Is it a COM / ActiveX server or does it use COM components? Does it link to other DLLs or use 3rd party libraries?

All these affect how you're going to port / rewrite from scratch your app. For example, if your code is an MFC app you may as well forget trying to salvage much code. If your app does http / high level networking stuff you may as well write from scratch. If your code low level you might have to refactor with some C# and some C++ accessible through a managed C++ layer.

Lots of choices and it really depends what your app is doing, how it was written etc.

locka
I'd really really prefer not to wrap it. Check my edits ;)
digEmAll
If it's just a basic app with STL, then I believe you're best to just write the equivalent in C#. I don't know of any machine translation tools, but they're not likely to produce good code. Use the original code as the inspiration, but things like collection classes, string manipulation, memory allocation etc. are sufficiently different that you probably wouldn't be doing a line for line port.Read this article for the sorts of issues a translation tool would have to deal with http://msdn.microsoft.com/en-us/library/ms379617%28VS.80%29.aspx
locka
Ok thanks for the link ;)
digEmAll
+1  A: 

It will cost you far more to convert it than to wrap it and pay a freelancer (like me) to help you out by changing the C++ code for you every few months (or every few years) when you need to make a change. There are some mechanical approaches but the bigger issue is that you can never really be sure that the new C# code does exactly what the old C++ code did. I have clients who have tried this and most gave up and threw the work away. The ones who succeeded did it very slowly, like this:

First, you wrap the old library and get your UI or whatever the new code is (web service, whatever) successfully calling the old library. This gets everyone some "bang for the buck" and buys you time to solve the "we can't maintain our old code" problem. You also develop a comprehensive test suite that proves what the old library does for various edge cases and strange things that only happen in the wild every few years. Over time, you move functionality from the old library into a new C# one and change the calling code to use the new library for that functionality. You move the most volatile parts, the things you change most often, out first. At every stage you run the test cases again to make sure your translation from C++ to C# didn't mess up the results it calculates. Maybe some of it you never move out, maybe in the end it is all moved. You stop when you feel the risk of being unable to maintain your own library and needing to pay someone to do it for you has dropped below the cost of continuing to translate it.

I recommend you have access to someone with good C++ skills when you start. You will probably run into things that don't make much sense to you. But you can get the value from the library pretty quickly, and still solve your underlying problem in the long term.

Kate Gregory
The "missing c++ skilled coders" is one but not the only reason for porting and not wrapping. Thanks for the hints though.
digEmAll