is it possible to pass a Byte[] from C# to unmanaged C++ and have it modified inside C++ reading its value back in C#? Something like:
C#:
[DllImport("MyDll.dll")]
public static extern bool UnmanagedFunction(ref Byte[] a, short b, ref ulong c);
...
bool success;
Byte[] a = new Byte[65];
ulong c = 0;
success = UnmanagedFunction(ref a, (short)a.Length, ref c);
C++:
extern "C" __declspec(dllexport) BOOL WINAPI UnmanagedFunction(
__inout_ecount(b) PBYTE a,
__in INT16 b,
__out DWORD c
) {
BOOL success = FALSE;
PBYTE uA;
OVERLAPPED d;
...
success = ReadFile(
readHandle,
uA,
b,
&c,
&d);
if ( success ) {
a = uA;
}
free(uA);
return success;
}
When I try to read the variable "a" on C# after the invocation of "UnmanagedFunction" it shows only the first position of the array, I'm sorry about the C code I'm really new to pointers and references in C++
I have tried to iterate through "uA" "c" times setting each position on "a", each position is defined but when the code returns to its managed section I get an exception telling me the memory is corrupt, something like:
for ( int i = 0; i < c; ++i )
a[i] = uA[i];
I have seen functions returning an IntPtr and then invoking the Marshal.Copy on the managed side, but I was wondering if would it be possible to something close to what I'm trying to achieve above
thanks in advance!