I have been struggling 2nd day with the task how to pass a structure, containing array of byte to c++ dll. Namely, I don't know how to marshal C# structure. Here is my code:
in C++ dll:
struct Image
{
int Width;
int Height;
int Depth;
uchar *Data;
};
.....
__declspec(dllexport) void DirectTransform(Image *InImage, Image*DestImage)
{
...Implementation...
}
in C# program:
[StructLayout(LayoutKind.Sequential)]
struct ImageData
{
public int Width;
public int Height;
public int Depth;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] Data;
}
[DllImport("MyDll.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern void DirectTransform(ImageData Src, ImageData Dest);
//Fill out both structures..
DirectTransform(Image, DestImage);
Exception throws at calling of DirectTransform and says that:
Cannot marshal field 'Data' of type'ImageData': Invalid managed/unmanaged type combination (Array fields must be paired with ByValArray or SafeArray).
When I change LPArray to ByValArray and point the size of array(in this case 202500) it also doesn't work, because the size is too large. When SafeArray is used, the program fails inside DLL with the message :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt. And data in structures are erroneous.
Could anyone help me?