如何调用 C++来自 C# 的类

发布于 2024-10-14 08:40:26 字数 1563 浏览 1 评论 0原文

我想我必须创建一个托管 C++ 代码来包装本机 C++。但我在尝试包装函数参数中使用的数组(其类型是在本机 C++ 中定义的)时遇到问题。原生C++代码如下:

//unmanageCPP.h
class __declspec(dllexport) unmanageMoney
{
public:
    unmanageMoney(int a, int b) { rmb = a; dollar = b; }
    unmanageMoney() { rmb = 0; dollar = 0; }
    int rmb;
    int dollar;
};

class __declspec(dllexport) unmanageSum
{
public:
    //how to wrap this funciton?
    int addDollar(unmanageMoney a[], unmanageMoney b[]);
};

//unmanageCPP.cpp
#include "unmanaged.h"

int unmanageSum::adddollar(unmanageMoney a[], unmanageMoney b[])
{
    return a[0].dollar + b[0].dollar;
}

谁能告诉我如何编写manageCPP.h?非常感谢!

更新

我按如下方式编写manageCPP.h,但我不知道如何编写addDollar()

//first, I wrap the class unmanageMoney for use in manageSum::addDollar()
public ref class manageMoney
{
private:
    unmanageMoney* mMoney;
public:
    unmanageMoney getMoney()
    {
        return *mMoney;
    }
    manageMoney(int a, int b)   { mMoney = new unmanageMoney(a, b); }
    ~manageMoney()  { delete mMoney; }
};

public ref class manageSum
{
    // TODO: Add your methods for this class here.
private:
    unmanageSum *mSum;
public:
    manageSum()
    {
        mSum = new unmanageSum();
    }
    ~manageSum()
    {
        delete mSum;
    }

    //it must be wrong if I code like this, for unmanageSun::adddollar() only
    // receives unmanageMoney as arguments. So what should I do?
    int adddollar(manageMoney a[], manageMoney b[])
    {
            return mSum->adddollar(a, b);
    }

};

I suppose I have to create a managed C++ code to wrap the native C++. But I have the problem while trying to wrap an array used in function parameter whose type is defined in native C++. The native C++ code is as follows:

//unmanageCPP.h
class __declspec(dllexport) unmanageMoney
{
public:
    unmanageMoney(int a, int b) { rmb = a; dollar = b; }
    unmanageMoney() { rmb = 0; dollar = 0; }
    int rmb;
    int dollar;
};

class __declspec(dllexport) unmanageSum
{
public:
    //how to wrap this funciton?
    int addDollar(unmanageMoney a[], unmanageMoney b[]);
};

//unmanageCPP.cpp
#include "unmanaged.h"

int unmanageSum::adddollar(unmanageMoney a[], unmanageMoney b[])
{
    return a[0].dollar + b[0].dollar;
}

Could anyone tell me how to write the manageCPP.h? Thanks very much!

Update

I compose the manageCPP.h as follows, but I don't know how to write addDollar()

//first, I wrap the class unmanageMoney for use in manageSum::addDollar()
public ref class manageMoney
{
private:
    unmanageMoney* mMoney;
public:
    unmanageMoney getMoney()
    {
        return *mMoney;
    }
    manageMoney(int a, int b)   { mMoney = new unmanageMoney(a, b); }
    ~manageMoney()  { delete mMoney; }
};

public ref class manageSum
{
    // TODO: Add your methods for this class here.
private:
    unmanageSum *mSum;
public:
    manageSum()
    {
        mSum = new unmanageSum();
    }
    ~manageSum()
    {
        delete mSum;
    }

    //it must be wrong if I code like this, for unmanageSun::adddollar() only
    // receives unmanageMoney as arguments. So what should I do?
    int adddollar(manageMoney a[], manageMoney b[])
    {
            return mSum->adddollar(a, b);
    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

Saygoodbye 2024-10-21 08:40:26

您可以使用 /clr 选项创建 C++/CLI 源文件

public ref class SomethingOrOther
{
    //...
};

并设置编译选项。

除此之外,它几乎与编写本机 C++ 相同。您将#include 想要重用的类的头文件、创建实例并调用其成员函数,就像普通的 C++ 一样。但 ref class 内的任何内容对于 C# 都是可见的。

并且您不要将 __declspec(dllexport) 放在类上。从来没有。它对于函数很有用,但与类一起使用时会造成痛苦。

You create a C++/CLI source file with

public ref class SomethingOrOther
{
    //...
};

and set the compile options to use the /clr option.

Beyond that, it's almost the same as writing native C++. You'll #include the header file for the class you want to reuse, create instances and call their member functions, just the same as normal C++. But anything inside that ref class will be visible to C#.

And you do NOT put __declspec(dllexport) on the class. Not ever. It's useful for functions, but creates misery when used with classes.

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