转换c源代码到c# [英] conversion c source code to c#

查看:87
本文介绍了转换c源代码到c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有一个用c语言编写的程序,我想把它转换为c#,但我的程序超过3000行......



所以请给我一些建议



有没有办法转换?

hi

i have a program in c language and i want to convert that to c# but my program is more than 3000 lines ...

so please give me some advices to do

is there any way to convert?

推荐答案

你永远不会得到你期望的答案。

1)据我所知,C没有.net启用版本,因此你不能使用中间语言作为帮助者

2)C具有比任何托管更低级别的概念语言

3)C不是OO,c#是OO语言 - 一个非常好的一个

4)用C语言思考完全不是用C语言思考#

您将找不到自动执行此翻译的方法。一般而言,根本不可能这样做。只有在特殊情况下,C#中的C应用程序才有可能相同。

顺便说一下,3000行代码并不多......
You will never get the answer you expect.
1) As I know C has no .net enabled edition, thus you can not use the intermediate language as helper
2) C has really lower level concepts than any managed language
3) C is not OO, c# is OO language - and a really good one
4) Thinking in C is completely other than thinking in C#
You will find no automatic means to do this translation. And in general it is also impossible to do it at all. Only in special cases there might be an equivalent of the C application in C#.
And by the way, 3000 lines of code are not that much...


转换的方法是从原始源代码或其他方法学习相关算法,然后在C#中实现应用程序。一些代码片段可能或多或少是字面翻译,语法略有不同,但大多数应该是完全不同的。作为回报,很有可能,您在C程序中发现的很多东西都可能被抛弃,因为它们已经在.NET库中实现了。这一切都很大程度上取决于原始程序的特点。例如,纯数值计算的很大一部分可能非常相似。



如果您希望采用某种自动转换方式 - 请忘记它。语言和特别是平台在概念和形式上都非常不同。



你可以考虑一些与翻译无关的替代方案。



第一个:您可以将原始应用程序重新编写为本机DLL,然后通过P / Invoke在C#应用程序中使用它。如果您需要学习P / Invoke,请参阅:

http://en.wikipedia。 org / wiki / P / Invoke [ ^ ],

http://msdn.microsoft.com/en-us/library/ Aa712982 [ ^ ]。



此CodeProject文章也可以提供帮助: Essential P /调用 [ ^ ] 。







回答后续问题:



上述引用对于使用非托管DLL(如果已经可用)非常有用。要在DLL和.NET程序集使用的导出函数中重新编写C代码,请从这里开始:

http://msdn.microsoft.com/en-us/library/1ez7dh12%28v=vs.100%29 [ ^ ],

http://msdn.microsoft.com/en-us/ library / z4zxe9k8%28v = vs.100%29.aspx [ ^ ]。



您可以在DLL创建方面找到更多帮助。确保只使用.NET可以理解的类型。如果您尝试仅使用基本类型,字符串和枚举,这很容易。除了字符串之外的结构和指针有点棘手。首先,尝试尽可能简单地保持DLL和.NET之间的接口。



[结束编辑]



第二个:您可以使用混合模式(托管+非托管)C ++ / CLI项目来嵌入现有的C代码。这样的项目可以或多或少地自由地混合使用C ++,C ++ / CLI和C代码。您可以将C代码包装在一些托管的ref类/结构中,并将它们公之于众。生成的可执行模块可以用作常规.NET程序集,由C#应用程序引用。请参阅:

http://en.wikipedia.org/wiki/C %2B%2B / CLI [ ^ ],

http://www.ecma-international .org / publications / standards / Ecma-372.htm [ ^ ],

http:// www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ],

http:// msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ]。



Go好运,

-SA
The way to convert is to learn the relevant algorithms from the original source code or otherwise and then implement the application in C#. Some fragments of code could be more or less literal translation with somewhat different syntax, but most should be deeply different. In return, chances are, a lot of things you will find in the C program could be thrown out, because the are already implemented in .NET libraries. It all strongly depends on the character of original program. For example, big part of pure numeric calculations could be very similar.

If you hoped for some "automatic" way of conversion — just forget it. The languages and especially platforms are very, very different, both conceptually and formally.

You can consider some alternatives not related to translation though.

First one: you can re-word original application into a native DLL and then use it in your C# application via P/Invoke. If you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject article can be also helpful: Essential P/Invoke[^].



In response to the follow-up questions:

The above references are useful to use unmanaged DLLs if they are already available. To re-work C code in DLL and export function to be used by .NET assemblies, please start from here:
http://msdn.microsoft.com/en-us/library/1ez7dh12%28v=vs.100%29[^],
http://msdn.microsoft.com/en-us/library/z4zxe9k8%28v=vs.100%29.aspx[^].

You can find a lot more help on DLL creation. Make sure you only use the types which .NET can understand. If you try to use only primitive types, strings and enumerations, this is easy. Structures and pointers other then strings is a bit more tricky. At first, try to keep interfaces between DLLs and .NET as simple as possible.

[END EDIT]

Second one: you can use mixed-mode (managed+unmanaged) C++/CLI project to embed existing C code. Such project can more or less freely mix C++, C++/CLI and C code. You can wrap your C code in some managed "ref" classes/structures and make them public. The resulting executable module can be used as a regular .NET assembly, referenced by your C# application. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

Good luck,
—SA


我没有自动转换方式 - 这是一项令人惊讶的困难工作自动执行,因为C#没有全局变量的概念,不希望你使用指针,并且与.NET框架紧密相关,因此C代码使用的函数将不适用于C#。



我怀疑你最好的方法是重写,而不是翻译,因为这两种语言在实践中出现的差别要大得多。



用Winston Churchill / George Bernard Shaw来解释:两种语言用相似的语法分隔。
There is no automatic way to convert that I am aware of - it is a surprisingly difficult job to do automatically, since C# has no concept of globals, does not want you to use pointers, and is strongly tied to the .NET framework so the functions your C code uses will not be applicable in C#.

I suspect your best approach will be a re-write, rather than a translation, as the two languages are a lot more different in practice that they appear.

To paraphrase Winston Churchill / George Bernard Shaw: Two languages separated by a similar syntax.


这篇关于转换c源代码到c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆