将数组参数从 C# 传递到 C++/CLI 方法
我对 C++/CLI 知之甚少,但我有一个简单的问题需要解决方案。我有一个 C++/CLI 类方法,它采用字节数组作为参数。该数组具有预先确定的长度,并且可以预先在C#中分配。该数组应该通过 C++/CLI 方法填充数据。
如何声明该方法然后从 C# 调用它?
我尝试在我的 C++/CLI 类中使用以下内容:
public ref class C
{
public:
void FillBytes(array<BYTE^>^ bytes);
};
然后,在 C# 中:
o = new C();
var bytes = new byte[3];
o.FillBytes(bytes);
但这根本不起作用:)。
I know only very little about C++/CLI, but I have a simple problem that needs a solution. I have a C++/CLI class method that takes a byte-array as a parameter. The array is of a pre-determined length, and can be allocated in C# beforehand. The array is supposed to be filled with data by the C++/CLI method.
How do I declare the method and then call it from C#?
I tried something like having the following in my C++/CLI class:
public ref class C
{
public:
void FillBytes(array<BYTE^>^ bytes);
};
And then, in C#:
o = new C();
var bytes = new byte[3];
o.FillBytes(bytes);
But that didn't work at all :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用
byte
而不是BOOL
引用?Have you tried using
byte
instead of aBOOL
reference?我知道这个问题很久以前就被问过,但希望它能帮助其他正在检查这篇文章的人。从 C++/CLI 中,您需要发送以下内容:
“帽子”表示它是 CLR 类型,% 表示您将通过引用传递它,以便 C++/CLI 填充数组。
然后你可以像这样从 C# 调用它:
希望它对某人有帮助!
I know this question was asked a long time ago but hopefully it will help someone else who is checking this post. From C++/CLI you need to send this:
The "hat" means its a CLR type and the % means you will pass it by reference so that the C++/CLI fills the array.
Then you can call it from C# like this:
Hope it helps someone!