I have a C# array<System::Byte>
and I want that to translate to a C++ byte*
. How can I do that? I am using C++/CLR because it lets me use managed/unmanaged code in the same project. I'm basically writing a DLL and making a few methods that can be called via C#, but that contain unmanaged C++ code.
So basically, my C++/CLR method header is this:
void testMethod(array<Byte>^ bytesFromCSharp);
and inside of that testMethod
I would like to translate the bytesFromCSharp
into a byte*
which can be used by other unmanaged C++ code. I malloced the byte*
array and wrote a for loop to copy byte by byte but it feels like there should be a better solution.
edit: Example of Hans' technique from his answer below:
//C#
byte[] myBytes = {1,2,3,4};
//C++/CLI
void testMethod(array<byte>^ myBytes) {
pin_ptr<byte> thePtr = &myBytes[0];
byte* bPtr = thePtr;
nativeCode(bPtr);
...
}