如何调用 C++来自 C# 的类
我想我必须创建一个托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
/clr
选项创建 C++/CLI 源文件并设置编译选项。
除此之外,它几乎与编写本机 C++ 相同。您将
#include
想要重用的类的头文件、创建实例并调用其成员函数,就像普通的 C++ 一样。但ref class
内的任何内容对于 C# 都是可见的。并且您不要将 __declspec(dllexport) 放在类上。从来没有。它对于函数很有用,但与类一起使用时会造成痛苦。
You create a C++/CLI source file with
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 thatref 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.