C++/CLI 将现有应用程序转换为托管代码
我有一个用 Borland C++ Builder 编写的现有应用程序。我的客户希望用 C#/WPF 重写它。这需要大量的工作,并且是一项复杂的任务,因为我需要重写整个(巨大的)算法。我正在考虑一种重用代码并能够在 C#/WPF 中仅创建 GUI 的方法。这可能吗?那容易吗?如何使 C++ 类对 .NET 可见?
如果您能给我一些链接/示例的简短答案,我将不胜感激。
I've got an existing application written in Borland C++ Builder. My client wants it rewritten in C#/WPF. This requires a lot of work and is a complex task because I need to rewrite the whole (huge) algorithm. I was thinking of a way to reuse the code and to be able to create only GUI in C#/WPF. Is this possible? Would that be easy? How can I make C++ classes visible to .NET ?
If you could give me brief answers with some links/examples I would be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将旧的 C++ 代码包装在 C++/CLI 包装器中,并将其构建到 DLL 文件中。然后它应该在任何 .NET 项目中可见。
根据您的算法/函数结构,这可能会略有变化,但基本形式以及我这样做的方式如下。请记住,这是一个非常基本的示例,但我尝试包含关键点:
1。定义 CLI 包装器
2.实现包装类
编写包装类并将其编译到类库后,您可以在 C# 项目中创建对 .DLL 文件的引用,并且 C# 应该看到托管包装类及其所有公共类功能。
另一件需要注意的事情是,如果您在 C++ 中创建新的托管对象,则使用关键字“gcnew”而不是“new”。因此,一个新字符串将是
String ^ someString = gcnew String();
最后,这里有一些有关 CLI 的一些链接,可能会有所帮助:
CLI - 维基百科
CLI - 代码项目
CLI - FunctionX
You can wrap your old C++ code in a C++/CLI wrapper and build it into a DLL file. It should then be visible in any .NET project.
Depending on your algorithm/function structure, this may change a little, but the basic form, and the way I did this, is the following. Keep in mind this is a VERY basic example, but I tried to include the key points:
1. Define the CLI wrapper
2. Implement the wrapper class
After you write the wrapper class and compile it into a class library you can create a reference to the .DLL file in a C# project and C# should see the managed wrapper class and all its public functions.
Another thing to note is that if you are creating new managed objects in C++ you use the keyword "gcnew" instead of "new". So a new string would be
String ^ someString = gcnew String();
Finally, here are some links to some things about CLI that may help:
CLI - Wikipedia
CLI - Code Project
CLI - FunctionX
嗯,据我所知,您可以使用互操作来解决这个问题。
事实上,最好继续使用经过长时间测试且稳定的代码。通过网络互操作服务,您可以在新应用程序和旧应用程序之间建立一座非常好的桥梁。
例子:
http://www.daniweb.com/forums/thread136041.html
http://msdn.microsoft.com/en -us/library/aa645736%28v=vs.71%29.aspx
http://www.codeguru.com/cpp/cpp/cpp_management /interop/article.php/c6867
Well, As far as i can remember by head, you can use interops for that matter.
It is in fact better to continue using a code that was for a long time tested, and is stable. With net Interop Services you can create a very good bridge between the new app and the old one.
examples:
http://www.daniweb.com/forums/thread136041.html
http://msdn.microsoft.com/en-us/library/aa645736%28v=vs.71%29.aspx
http://www.codeguru.com/cpp/cpp/cpp_managed/interop/article.php/c6867