Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a project from C++ to C#

Tags:

c++

c#

I've got a medium scale project (a turn-based game) that's currently written in C/C++. Only one person is working on it. It will complie with /clr, although I haven't tried /clr:pure.

I'm looking for advice on the overall process of converting it to C#.

It's mainly C with a thin veneer of C++ on it (C with static/singleton classes for scoping). Yes, it's not 'real' OO, I've been planning to do the C# conversion first to avoid creating any conversion issues while converting.

It makes very limited use of the STL (The queue class) in the pathfinding module, and because it's a game, it also doesn't do any memory allocation outside of the third party sound library in a DLL, and what's necessary for loading the graphics bitmaps.

I want to convert it into idiomatic C#. I'd rather not hold a discussion on whether this is a good idea, I understand that it isn't, let's please let part go. Assume that I've got overriding reasons please.

I've also done my research, and there is a thread that is somewhat relevant about converting the methods using reflector. I plan to look more deeply into it when I get a chance.

Translate C++/CLI to C#

There is also one pay applicaiton that will convert from C++ to C# that I may look at if that doesn't work as well as I'd like.

The hardest part is going to be rewriting the interface in either WPF/Silverlight or XNA (or both) There are pros and cons to each one, but I'm leaning towards WPF right now because of the font support, and because that way I won't have to write all the widgets. I may end up doing both, an XNA quick port, and a WPF one later.

There are several possible approaches to this, and I wanted to know if anyone had any experience with a conversion like this, and any suggestions or pitfalls.

1) Create the UI first, This involves either leaving the Graphics module as is, and converting from GDI to raw GDI like calls in XNA, and converting later to WPF one dialog at a time.

2) Convert the guts first, and leave the main UI in C++/CLI for now, after the guts are converted, switch over the interface one dialog at a time to WPF.

A related question is whether it's worth doing this module by module, or basically all at once, and whether it's better to do a rough conversion into C# and clean everythig up, or clean up everything in C++ then convert to C#.

The thought right now is to rewrite the event loops to use a home grown common loop like MFC, then try to convert everything at once to C#. Leave the graphics in C++ and see what breaks. After that, move to XNA and provide a WPF layer later. The last two steps are arbitrary, I think that the XNA port is going to be simpler, but using WPF basic panel may be pretty simple too.

I'm open to any suggestions that may help.

Thanks, Ralph

like image 595
Ralph Trickey Avatar asked Jun 22 '09 15:06

Ralph Trickey


3 Answers

I must say that the best best advice I can give you is to rewrite the application. C++ and C# are so different that if you are certain that you want to convert the entire thing then a rewrite will most likely be your best option.

I guess my main question is why do you want to convert this project to C#? I understand that this is not your question but since you are undertaking what I consider to be a considerable effort to do this, I hope that you realize what you are getting into.

like image 186
Andrew Hare Avatar answered Oct 24 '22 11:10

Andrew Hare


I found out that CodeRush (which I already own) has a 'smart paste' operation which does a reasonable job of converting what can be converted. There's also a CR_Paste add-in on googleplex which does something similar (the CR_Paste add-in may not require Coderush, only the free DXCore application).

Since I'll have full access to the parse tree (and understand parse trees), I may (and may not) customize it to change char * to String, etc. If I do much customization, I'll probably create an open source version, probably on CodePlex.

like image 31
Ralph Trickey Avatar answered Oct 24 '22 12:10

Ralph Trickey


With Visual Studio 2013 Express

You can actually, open two Windows of it (lets say VSEx 2013 (1) and VSEx 2013 (2)).

You open it first, you open your existing project, than : you right click on its icon in your taskbar, and click on Visual Studio 2013 Express... This will open a second time the application, you'll have it run twice at the same time.

Because the reason I falled on your post... lol... is because i was looking for the same.

What I did next was nice.

Modify : Select All Copy...

Go on the other "window" of VS Express 2013, Paste...

Then, of course, you might get mini errors like colors having to be reset, forms to be redefined, and all the :: and -> to "modify replace : :: by . and -> by . (You'll probably have to recreate all "Events" though, but normally : all default properties will be set as you set them on the original settings.

Then, if you got errors at paste like (object cannot be found), that mostly mean one of your control is actually a .COM or embeded thing... Just add it as you did in the older versions of Visual C++ (example : show : toolbox, then right click in it and Add ITems (or choose items) and then add it normally)..

Hope this helps.

(I'm actually redoing all my VC++ 2010 program, in C# 2013.)

Have fun :)

like image 2
FenixEden Avatar answered Oct 24 '22 11:10

FenixEden