从C ++到C#的转换问题 [英] C++ to C# conversion issue

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

问题描述

我们有一个用C编写的程序,该程序正在使用15年的C ++ exe中调用函数.我想使用暴露与C ++ exe相同功能的C#重写此C ++ exe.到目前为止我还不清楚如何做到这一点.请注意,C#应用程序必须是一个exe,而不是dll,因为它需要在后台运行.

解决方案

请参阅我对问题的评论.你应该先考虑一下.

从C ++到C#的转换"的整个思想是错误的. 没有转换"之类的东西.语言的性质不同,它们在不同的平台,不同类型的平台上使用不同的概念.人们无法定义什么是转换"甚至翻译.但是,从特定应用程序的角度来看,您始终可以以某种方式翻译代码,或使用旧代码作为参考重新实现它;这完全取决于应用程序和要求.

在所有情况下,在这样做之前,您都应该考虑以下替代方法: 1)使用C ++代码, 2)使用C ++代码的组合,导出一些函数并通过P/Invoke在您的C#项目中使用它们, 3)将您的C ++代码转换为C ++/CLI并将其用作.NET程序集; 4)使用C ++和C ++/CLI将您的C ++代码转换为混合模式(托管+非托管)项目,重用大多数旧的C ++代码,包装C ++中所需的方法甚至类/CLI管理"ref"类,并将其作为任何其他.NET项目中的常规.NET程序集进行引用.

在真正了解所有替代方案和涉及的技术之前,请不要告诉我们必须如此".在理解之前,您不能认为您的决定有效或相关.

如果您需要学习P/Invoke,请参阅:
http://en.wikipedia.org/wiki/Platform_Invocation_Services [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

此CodeProject教程文章也可能有用:
基本P/调用 [ http://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],
http://www.ecma-international.org/publications/standards/Ecma-372.htm [^ ],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ],
http://www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ].

-SA


^ ]

如何在C#中创建Windows服务 [ IPC [ 解决方案

Please see my comment to the question. You should think about it first.

The whole idea of "conversion" from C++ to C# is wrong. There is no such thing as "conversion". The languages are different in their nature, work on different platforms, on different types of platforms, using very different concepts. One could not define what is a "conversion" or even a translation. However, from the standpoint of a particular application, you can always somehow translate the code, or re-implement it using old code as a reference; it totally depends on the application and requirements.

In all cases, before doing so, you should think about the following alternatives: 1) use C++ code, 2) use combination of C++ code, export some function and use them in your C# project via P/Invoke, 3) translate your C++ code into C++/CLI and use it as the .NET assembly; 4) translate your C++ code into mixed-mode (managed + unmanaged) project using C++ and C++/CLI, re-use most of your old C++ code, wrap the methods or even classes you need in C++/CLI managed "ref" classes and reference this as a regular .NET assembly in any other .NET projects.

Don''t tell us "it must be" before you really understand all the alternatives and the techniques involved. You cannot consider your decision as valid or even relevant before you understand it.

In you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

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

Information on C++/CLI:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

—SA


What is a Windows service[^]

How to create a windows service in C#[^]

IPC[^]


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

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