Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array<System::Byte> to char* in C++ CLR?

Tags:

c++

c#

c++-cli

In my project, I pass a byte[] from C# to C++ CLR function.

C++ CLR code:

void TestByteArray(array<System::Byte>^ byteArray)
{
    ...
}

C# code:

byte[] bytes = new byte[128];
...
TestByteArray(bytes);

In the TestByteArray() function, I need convert byteArray to char*, so that I can used it in native C++ code. How can I do such conversion?

like image 518
Spark Avatar asked Oct 10 '11 02:10

Spark


1 Answers

void TestByteArray(array<System::Byte>^ byteArray)
{
    pin_ptr<System::Byte> p = &byteArray[0];
    unsigned char* pby = p;
    char* pch = reinterpret_cast<char*>(pby);

    // use it...
}
like image 64
Ben Voigt Avatar answered Sep 28 '22 18:09

Ben Voigt