Converting a project from C++ to C#

2020-07-11 08:11发布

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

标签: c# c++
6条回答
Fickle 薄情
2楼-- · 2020-07-11 08:44

I found out that CodeRush (which I already own) has a 'smart paste' operation which does a reasonable job of converting what can be conferted. 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 applicattion.

Since I'll have full acess 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.

Ralph

查看更多
劳资没心,怎么记你
3楼-- · 2020-07-11 08:52
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    char a;//letra
    int num;//cantidad a imprimir

    //Ingresa los datos
    cout<<"Introduce la letra "<<endl;
    cin>>a;
    cout<<"Introduce la cantidad para imprimir "<<endl;
    cin>>num;

    //imprime de 5 a 1
    for(int i=num; i>0; i--)
    {
        for(int c=0; c<i; c++)
            cout<<a;
        cout<<endl;
    }

    //imprime de 1 a 5
    for(int i=0; i<num; i++)
    {
        for(int c=0; c<=i; c++)
            cout<<a;
        cout<<endl;
    }
    return 0;
}
查看更多
Explosion°爆炸
4楼-- · 2020-07-11 09:02

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.

查看更多
狗以群分
5楼-- · 2020-07-11 09:04

I am busy converting C++ code to c# code.

I am finding it much faster and easier than I had thought. and yes I also first want it to be identical before making changes in some cases such as scientific algorithms you want identical output, if no bench marks exists.

Well since your code is more c based this is going to just give you spagetti c# code that you need to refactor later into classes. I guess you will have a lot of static methods initially that were your c functions. Try to bundle some data and methods together where they describe objects behaviour.

I am doing it is file by file, line by line. starting with the guts. When I find something completly outdated I replace it with the c# equivalent, using Lists , IEnumerables etc. Some of the low level calcuations can be replaced with C# standard libraries.

I would advice against using unsafe C#. There is often an easier and saver way around it. Pointer calcuations often can be kept very similar to C++ code and are some of the fastest things to translate.

e.g.

double* array_p;

converts to

double[] array_p

now so long as you referenced array_p[i] afterwards where i is an int ,that code stays the same if you did pointer incrementing array_p ++ , make use of an int, that you start at 0 and increment. e.g. i++. and call

array_p[i]

Passing Pointers to pointers from one method to another e.g. *array_p is just passing a reference to array_p in c#. Essentially in c# all classes get passed by reference in any case so there is not that much need to have all these pointers.

The gui I would write from scratch though. OO and non-OO just too different for this.

Good luck. Test the low level code and clean it up where possible.

查看更多
手持菜刀,她持情操
6楼-- · 2020-07-11 09:05

It sounds like you need to redesign the app, not just a code conversion. Converting a non-OO C/C++ app to C# doesn't seem like a realistic goal.

查看更多
贼婆χ
7楼-- · 2020-07-11 09:11

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 :)

查看更多
登录 后发表回答