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

14,399
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...
}
Share:
14,399
Spark
Author by

Spark

I choose I like!

Updated on June 09, 2022

Comments

  • Spark
    Spark almost 2 years

    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?

  • Ben Voigt
    Ben Voigt over 12 years
    No, that would be for wchar_t (the C++ equivalent to System::Char).