I am using the following code to marshal an array of structs to c++:
[DllImport("IPD.dll", EntryPoint = "process", CallingConvention = CallingConvention.Cdecl)]
public static extern Pixel* process(Pixel* pixels, int numPoints, uint processingFactor);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Pixel
{
public fixed byte x[3];
public uint numebrOfPixels;
}
...
Pixel[] pixels = extractPixels(image);
fixed (Pixel* ptr = pixels)
{
Pixel* result = process(ptr, pixels.Length,processingFactor);
}
In order to populate my struct I am using the following code:
//Looping and populating the pixels
for(i=0;i<numOfPixels;i++)
{
fixed (byte* p = pixels[i].x)
{
p[0] = r;
p[1] = g;
p[2] = b;
}
}
The code works just fine with no memory leaks.
How can I be sure the during marshaling the pixels to the native code the CLR doesn't copy the pixels array back and forth?
Cheers,
Doron
Pixel*are distinct pixels from the one passed as an input parameter, then it is probably better to pass them as an input parameter as a secondPixel* pixelOut, so that C#-side you can have aPixel[]even for the output pixels.