I have a C# function with following signature:
int Get1251Bytes(string source, byte[] result, Int32 lengthOfResult)
I call it from C++. I was informed by compiler that 2-nd param must have SAFEARRAY* type. So I call it in this way:
SAFEARRAY* safeArray = SafeArrayCreateVector(VT_UI1, 0, arrayLength);
char str[] = {'s', 't', 'a', 'c', 'k', '\0'};
converter->Get1251Bytes(str, safeArray, arrayLength);
But safeArray is not updated, it still contains zores. But I tested Get1251Bytes function in C# unit-test. It works properly and updates result array. What am I doing wrong?
Your problem is related to Blittable and Non-Blittable Types (
Byteis blittable):To fix your code you need to apply an
[Out]attribute to theresultparameter in the C# code:Also, you don’t need to pass
lengthOfResult. In .NET you can use theLengthproperty to get the size of the array.