I created 2 programs – in C# and C++, both invoke native methods from C dll. C++ works fine, because there are the same data types, C# doesn’t work.
And native function parameter is unsigned char*. I tried byte[] in C#, it didn’t work, then I tried:
fixed(byte* ptr = byte_array) {
native_function(ptr, (uint)byte_array.Length);
}
It also doesn’t work. Is it correct to convert byte array to byte* in such way? Is it correct to use byte in C# as unsigned char in C?
EDIT:
This stuff returns the wrong result:
byte[] byte_array = Encoding.UTF8.GetBytes(source_string);
nativeMethod(byte_array, (uint)byte_array.Length);
This stuff also returns the wrong result:
byte* ptr;
ptr = (byte*)Marshal.AllocHGlobal((int)byte_array.Length);
Marshal.Copy(byte_array, 0, (IntPtr)ptr, byte_array.Length);
You have to marshal the
byte[]: