Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a byte[] to C# in C++ CLR

Tags:

c#

c++-cli

I'm using C++ CLR to wrap a native C++ dll. So that the C++ CLR dll can be accessed by a C# project.

The problem is that when I want to return a byte[] to C#, and write such code in CLR:

static System::Byte[]^ GetTestByteBuffer()
{
    System::Byte[]^ byte = gcnew System::Byte[128];
    return byte;
}

but it cannot pass compilation. Anyone can help me?

compilation error:

error C3409: empty attribute block is not allowed 
error C3409: empty attribute block is not allowed error C2146: syntax error "^": 
error C2334: unexpected token(s) preceding '{'; skipping apparent function
like image 319
Spark Avatar asked Oct 10 '11 01:10

Spark


People also ask

How do you return bytes?

The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size. The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

What is byte array in C?

In C, a byte array is considered an object, by C's definition of object: 3.15. 1 object. region of data storage in the execution environment, the contents of which can represent values. 2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.

How do you reverse Bytearray?

you can use the linq method: MyBytes. Reverse() as well as the Array. Reverse() method.

What is the byte array?

What is a Bytearray? A byte array is simply a collection of bytes. The bytearray() method returns a bytearray object, which is an array of the specified bytes. The bytearray class is a mutable array of numbers ranging from 0 to 256.


1 Answers

This is the way you declare a byte array in C++/CLI:

array<System::Byte>^

Google is your friend...

like image 51
Jeff Avatar answered Oct 02 '22 07:10

Jeff