将项目从 C++ 转换为 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1027785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:52:06  来源:igfitidea点击:

Converting a project from C++ to C#

c#c++

提问by Ralph Trickey

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.

我有一个目前用 C/C++ 编写的中型项目(回合制游戏)。只有一个人在做这件事。它将符合 /clr,虽然我还没有尝试过 /clr:pure。

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

我正在寻找有关将其转换为 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.

它主要是 C,上面有一层薄薄的 C++(C 带有用于范围界定的静态/单例类)。是的,这不是“真正的”OO,我一直计划先进行 C# 转换,以避免在转换时产生任何转换问题。

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.

它在寻路模块中对 STL(队列类)的使用非常有限,并且因为它是一个游戏,它也不会在 DLL 中的第三方声音库之外进行任何内存分配,以及加载所需的内容图形位图。

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.

我想把它转换成惯用的 C#。我宁愿不讨论这是否是一个好主意,我知道它不是,让我们放手吧。请假设我有压倒一切的理由。

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#

将 C++/CLI 转换为 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.

还有一个付费应用程序可以从 C++ 转换为 C#,如果它不能像我想要的那样工作,我可能会查看它。

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.

最难的部分将是在 WPF/Silverlight 或 XNA(或两者)中重写界面 每种方法都有优点和缺点,但由于字体支持,我现在倾向于 WPF,因为那样我不必编写所有小部件。我最终可能会同时做两个,一个 XNA 快速端口,然后一个 WPF。

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.

1) 首先创建 UI,这涉及要么保持 Graphics 模块原样,然后从 GDI 转换为原始 GDI(如 XNA 中的调用),然后一次转换为 WPF 一个对话框。

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.

2)先转换胆量,暂时将主UI留在C++/CLI中,转换胆量后,将界面一次一个对话框切换到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#.

一个相关的问题是是否值得一个模块一个模块地做这个模块,或者基本上一次完成,以及是否更好地将粗略转换为 C# 并清理所有内容,或者在 C++ 中清理所有内容然后转换为 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.

现在的想法是重写事件循环以使用像 MFC 这样的本土通用循环,然后尝试将所有内容一次转换为 C#。将图形保留在 C++ 中,看看有什么问题。之后,移至 XNA 并稍后提供 WPF 层。最后两步是任意的,我认为 XNA 端口会更简单,但使用 WPF 基本面板可能也很简单。

I'm open to any suggestions that may help.

我愿意接受任何可能有帮助的建议。

Thanks, Ralph

谢谢,拉尔夫

采纳答案by Ralph Trickey

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.

我发现 CodeRush(我已经拥有)有一个“智能粘贴”操作,它可以合理地转换可以授予的内容。googleplex 上还有一个 CR_Paste 插件,它执行类似的操作。(CR_Paste 插件可能不需要 Coderush,只需要免费的 DXCore 应用程序。

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.

由于我将完全访问解析树(并了解解析树),因此我可能(也可能不会)对其进行自定义以将 char * 更改为 String 等。如果我进行了大量自定义,我可能会创建一个开源版本,可能在 CodePlex 上。

Ralph

拉尔夫

回答by Andrew Hare

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.

我必须说,我能给你的最好的建议是重写应用程序。C++ 和 C# 是如此不同,如果您确定要转换整个内容,那么重写很可能是您的最佳选择。

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 considerableeffort to do this, I hope that you realize what you are getting into.

我想我的主要问题是为什么要将此项目转换为 C#?我知道这不是您的问题,但由于您正在进行我认为是相当大的努力来做到这一点,我希望您意识到自己在做什么。

回答by alexD

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.

听起来您需要重新设计应用程序,而不仅仅是代码转换。将非 OO C/C++ 应用程序转换为 C# 似乎不是一个现实的目标。

回答by Gregor

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

我正忙于将 C++ 代码转换为 C# 代码。

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.

好吧,因为您的代码更多地基于 c,所以这只会为您提供意大利面条 c# 代码,您稍后需要将这些代码重构为类。我猜你最初会有很多静态方法,它们是你的 c 函数。尝试将一些描述对象行为的数据和方法捆绑在一起。

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.

我正在逐个文件,一行一行地做。从胆量开始。当我发现一些完全过时的东西时,我将它替换为 C# 等效项,使用 Lists 、 IEnumerables 等。一些低级计算可以替换为 C# 标准库。

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.

我建议不要使用不安全的 C#。通常有一种更简单、更省钱的方法来解决它。指针计算通常可以保持与 C++ 代码非常相似,并且是一些转换速度最快的东西。

e.g.

例如

double* array_p;

双* array_p;

converts to

转换为

double[] array_p

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

现在只要您在 i 是 int 之后引用了 array_p[i] ,那么如果您执行指针递增 array_p ++ ,则代码保持不变,使用 int,从 0 开始并递增。例如我++。并打电话

array_p[i]

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.

将指针从一个方法传递到另一个方法,例如 *array_p 只是在 c# 中传递对 array_p 的引用。本质上,在 c# 中,所有类在任何情况下都是通过引用传递的,因此没有那么多需要拥有所有这些指针。

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

不过,我会从头开始编写 gui。OO 和非 OO 对此差别太大了。

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

祝你好运。测试低级代码并在可能的情况下对其进行清理。

回答by FenixEden

With Visual Studio 2013 Express

使用 Visual Studio 2013 Express

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

实际上,您可以打开它的两个窗口(比如 VSEx 2013 (1) 和 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.

首先打开它,然后打开现有项目,然后:在任务栏中右键单击它的图标,然后单击 Visual Studio 2013 Express...这将第二次打开该应用程序,您将让它运行两次同时。

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...

继续 VS Express 2013 的另一个“窗口”,粘贴...

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.

然后,当然,您可能会遇到一些小错误,例如必须重置颜色,重新定义表单,以及所有 :: 和 -> 以“修改替换 :: :: by . 和 -> by .(您可能有虽然要重新创建所有“事件”,但通常情况下:所有默认属性都将在您在原始设置上设置它们时进行设置。

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

然后,如果您在粘贴时遇到错误(找不到对象),这主要意味着您的控件之一实际上是 .COM 或嵌入的东西......只需像在旧版本的 Visual C++ 中一样添加它(例如:显示:工具箱,然后右键单击它并添加项目(或选择项目),然后正常添加)。

Hope this helps.

希望这可以帮助。

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

(我实际上是在 C# 2013 中重做我所有的 VC++ 2010 程序。)

Have fun :)

玩得开心 :)

回答by Daniela Moya

#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;
}