通过引用从c#传递字节数组到托管c ++ [英] pass byte-array by reference from c# to managed c++

查看:87
本文介绍了通过引用从c#传递字节数组到托管c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在C#中用byte []分配一个字节数组byteArray = new byte [5000];


现在我想通过引用将这个byte-Array传递给托管C ++ - 函数,

这样我就可以更改byteArray的内容,然后看看

C#中的变化。

(如果你想了解更多细节:目标是将c ++中现有的char-array的内容精确地写入传递的byteArray中,所以那个函数在C#中使用了byteArray的内容之后的
等于
C ++ char-Array的内容。)


所以我希望你明白我将要做的事 - 不幸的是,我没有

如何做到这一点的线索。

但是,我感谢任何帮助,


by ekim

解决方案

Ekim< th ****** ******@gmx.net>写道:

我在C#中使用byte []分配一个字节数组byteArray = new byte [5000];

现在我要传递这个字节数组通过引用来管理C ++ - 函数,
这样我就可以更改byteArray的内容,然后看看C#中的更改。
(如果你想了解更多细节) :目标是将c ++中现有的char-array的内容精确地写入传递的byteArray中,以便在函数处理完C#中的byteArray内容后等待
char-C ++数组的内容。)

所以我希望你能理解我将要做的事 - 不幸的是,我没有了解如何做到这一点。



我认为你根本不需要通过引用传递byteArray - 你会

只有在你尝试时才需要这样做更改字节的值

数组本身 - 即将其更改为完全不同的数组。


我wou ld *希望*您可以通过值传递引用(因为

正常)并在C代码中将其作为char *接收,但我远远不是

互操作专家。您是否尝试过询问.interop小组?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

Ekim< th ************ @ gmx.net>写道:

我在C#中使用byte []分配一个字节数组byteArray = new
byte [5000];
现在我要传递这个字节数组到托管C ++ - 函数
引用,这样我就可以改变byteArray的内容,然后
看到C#中的变化。
(如果你想知道更多细节) :目标是将c ++中现有的char数组的内容精确地写入传递的byteArray中,以便在函数处理完C#
中byteArray的内容后等于char-C ++数组的内容。)

所以我希望你能理解我将要做的事 - 不幸的是,我没有了解如何做到这一点。


我认为你根本不需要通过引用传递byteArray - 只有当你试图改变byte的值时才需要这样做/>数组本身 - 即将其更改为完全不同的数组。

我希望*你可以j我按值传递引用(正常),并在C代码中将其作为char *接收,但我远离
互操作专家。您是否尝试过询问.interop组?




是的,我尝试在.interop-ng中询问,但现在没有答案; - (

是的,事实上我只有必须改变byteArray的内容,而不是你想象的那个

地址或大小。

但是我也不知道该怎么做!?


ekim


最近,我尝试使用

int dummy(字节数组__gc [])// c ++函数

{

将charArray复制到byteArray中

}


并从C#调用它像

byte [] array = new byte [5000]; //分配新的byte-Array

dummy(ref array); //调用c ++ - 函数

//在这个地方,byteArray-content应该与我复制的

charArray内容相同


顺便说一句,我在C#中尝试过,有没有ref - 没有工作。


关于它的奇怪之处在于,当我尽快调试我的应用程序时得到

调用,byteArray以某种方式被截断为长度1(而不是

5000)。

有人知道我做错了吗?


hello,
I''m allocating a byte-Array in C# with byte[] byteArray = new byte[5000];

Now I want to pass this byte-Array to a managed C++-function by reference,
so that I''m able to change the content of the byteArray and afterwards see
the changes in C#.
(if you wanna know more details: the goal is to write the content of an
existing char-array in c++ precisely into the passed byteArray, so that
after the function has proceded the content of the byteArray in C# equals
the content of the char-Array of C++.)

So I hope you understand what I''m about to do - unfortunately, I have no
clue how to do this.
However, I appreciate any help,

by ekim

解决方案

Ekim <th************@gmx.net> wrote:

I''m allocating a byte-Array in C# with byte[] byteArray = new byte[5000];

Now I want to pass this byte-Array to a managed C++-function by reference,
so that I''m able to change the content of the byteArray and afterwards see
the changes in C#.
(if you wanna know more details: the goal is to write the content of an
existing char-array in c++ precisely into the passed byteArray, so that
after the function has proceded the content of the byteArray in C# equals
the content of the char-Array of C++.)

So I hope you understand what I''m about to do - unfortunately, I have no
clue how to do this.



I don''t think you need to pass byteArray by reference at all - you''d
need to do that only if you were trying to change the value of byte
array itself - i.e. change it to a completely different array.

I would *hope* that you could just pass the reference by value (as
normal) and receive it as char* in the C code, but I''m far from an
interop expert. Have you tried asking on the .interop group?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Ekim <th************@gmx.net> wrote:

I''m allocating a byte-Array in C# with byte[] byteArray = new byte[5000];
Now I want to pass this byte-Array to a managed C++-function by reference, so that I''m able to change the content of the byteArray and afterwards see the changes in C#.
(if you wanna know more details: the goal is to write the content of an
existing char-array in c++ precisely into the passed byteArray, so that
after the function has proceded the content of the byteArray in C# equals the content of the char-Array of C++.)

So I hope you understand what I''m about to do - unfortunately, I have no
clue how to do this.



I don''t think you need to pass byteArray by reference at all - you''d
need to do that only if you were trying to change the value of byte
array itself - i.e. change it to a completely different array.

I would *hope* that you could just pass the reference by value (as
normal) and receive it as char* in the C code, but I''m far from an
interop expert. Have you tried asking on the .interop group?



Yes, I tries asking in the .interop-ng, but no answers ''til now ;-(
yeah in fact I "only" have to change the content of the byteArray, not its
address or size as you might think.
But I don''t know how to do that also!?

ekim


Lately, I tried using a
int dummy(Byte array __gc[]) // c++ function
{
copy charArray into byteArray
}

and calling it from C# like
byte[] array = new byte[5000]; // allocate new byte-Array
dummy(ref array); // call c++-function
// in this place, the byteArray-content shall be the same as the
charArray-content I copied from

by the way, I tried it in C# with and without "ref" - neither did work.

the queer thing about it is, when I debug my app as soon as "dummy" gets
called, the byteArray is somehow truncated to the length 1 (instead of
5000).
does somebody know what I did wrong?


这篇关于通过引用从c#传递字节数组到托管c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆