C++/CLI 将现有应用程序转换为托管代码

发布于 2024-10-08 15:56:17 字数 199 浏览 1 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-10-15 15:56:17

您可以将旧的 C++ 代码包装在 C++/CLI 包装器中,并将其构建到 DLL 文件中。然后它应该在任何 .NET 项目中可见。

根据您的算法/函数结构,这可能会略有变化,但基本形式以及我这样做的方式如下。请记住,这是一个非常基本的示例,但我尝试包含关键点:

1。定义 CLI 包装器

using namespace System;

#include "myAlgorithmClass"

public ref class MyCLIWrapperClass //the 'ref' keyword specifies that this is a managed class
{
public:

    MyCLIWrapperClass(); //Constructor

    //function definitions are the same, though some types,
    //like string, change a little. Here's an example:
    String ^ GetAString(); //the "String" is the .NET "System.String" and the ^ is sort of a pointer for managed classes

    //int, double, long, char, etc do not need to be specified as managed.
    //.NET code will know how to handle these types.

    //Here you want to define some functions in the wrapper that will call
    //the functions from your un-managed class. Here are some examples:
    //Say that your functions in the Algorithm class are int Func1, double Func2, and std::string Func3:
    int Func1();
    double Func2();
    String ^ Func3();

private:
    //All private functions and members here
    myAlgorithmClass algor;
};

2.实现包装类

using namespace System;
#include <string> //For the string function ex. below
#include "myAlgorithmClass"

MyCLIWrapperClass::MyCLIWrapperClass()
{
    algor = myAlgorithmClass(); //create instance of your un-managed class
}

int MyCLIWrapperClass::Func1()
{
    return algor.Func1(); //Call the un-managed class function.
}

double MyCLIWrapperClass::Func2()
{
    return algor.Func2(); //Call the un-managed class function.
}

String ^ MyCLIWrapperClass::Func3()
{
    //Converting a std::string to a System.String requires some conversion
    //but I'm sure you can find that somewhere on Google or here on SO

    String ^ myString = "";
    std::string myUnmanagedString = algor.Func3(); //Call the un-managed class function.

    //Convert myUnmanagedString from std::string to System.String here

    return myString;
}

编写包装类并将其编译到类库后,您可以在 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

using namespace System;

#include "myAlgorithmClass"

public ref class MyCLIWrapperClass //the 'ref' keyword specifies that this is a managed class
{
public:

    MyCLIWrapperClass(); //Constructor

    //function definitions are the same, though some types,
    //like string, change a little. Here's an example:
    String ^ GetAString(); //the "String" is the .NET "System.String" and the ^ is sort of a pointer for managed classes

    //int, double, long, char, etc do not need to be specified as managed.
    //.NET code will know how to handle these types.

    //Here you want to define some functions in the wrapper that will call
    //the functions from your un-managed class. Here are some examples:
    //Say that your functions in the Algorithm class are int Func1, double Func2, and std::string Func3:
    int Func1();
    double Func2();
    String ^ Func3();

private:
    //All private functions and members here
    myAlgorithmClass algor;
};

2. Implement the wrapper class

using namespace System;
#include <string> //For the string function ex. below
#include "myAlgorithmClass"

MyCLIWrapperClass::MyCLIWrapperClass()
{
    algor = myAlgorithmClass(); //create instance of your un-managed class
}

int MyCLIWrapperClass::Func1()
{
    return algor.Func1(); //Call the un-managed class function.
}

double MyCLIWrapperClass::Func2()
{
    return algor.Func2(); //Call the un-managed class function.
}

String ^ MyCLIWrapperClass::Func3()
{
    //Converting a std::string to a System.String requires some conversion
    //but I'm sure you can find that somewhere on Google or here on SO

    String ^ myString = "";
    std::string myUnmanagedString = algor.Func3(); //Call the un-managed class function.

    //Convert myUnmanagedString from std::string to System.String here

    return myString;
}

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

岁月无声 2024-10-15 15:56:17

嗯,据我所知,您可以使用互操作来解决这个问题。
事实上,最好继续使用经过长时间测试且稳定的代码。通过网络互操作服务,您可以在新应用程序和旧应用程序之间建立一座非常好的桥梁。

例子:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文