Passing array argument from C # to C ++ / CLI method

I know 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 has a predefined length and can be pre-assigned in C #. The array is assumed to be populated with data using a C ++ / CLI method.

How do I declare a method and then call it from C #?

I tried something like 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 it didn't work at all :).

+2


source to share


2 answers


Have you tried using byte

instead of link BOOL

?



void FillBytes(array<System::Byte>^ bytes);
                                ↑
                           //   no ^ here

      

+9


source


I know this question has been asked a long time ago, but hopefully it helps someone else who is checking this post. From C ++ / CLI, you need to send this message:

void FillBytes(array<unsigned char>^% bytes);

      

"Hat" means its CLR type, and% means you pass it by reference so that C ++ / CLI will populate the array.



Then you can call it from C # like this:

o = new C();
var bytes = new byte[3];
o.FillBytes(ref bytes);

      

Hope this helps someone!

+6


source







All Articles