Roger Breton

Roger Breton

  • NA
  • 2
  • 1k

Passing a pointer to an array to an external function

Jul 8 2016 1:10 PM
I want to use lcms2.DLL library from C#.
At the time of calling the DLL function, which convert pixels data, I get an AccessViolation error.
 
I need to pass two pointers to the function, a pointer to an inputBuffer and a pointer to an outputBuffer which I have defined as Byte[].
 
This is the DLLimport :
 
[DllImport(@lcms2Path)]
public static extern void cmsDoTransform(
[In] IntPtr xform,
[In] byte[] InputBuffer,
[Out] byte[] OutputBuffer,
[In] UInt32 Size);
 
And this is my method call:
// 1 -- read an image into BitmapFrame 
BitmapFrame myBitmapFrame = decoder.Frames[0];
 
// 2 -- copy image data to inputBuffer byte array 
Byte[] inputBitmapData = new Byte[stride * myBitmapFrame.PixelHeight];           
BitmapFrame.CopyPixels(inputBitmapData, stride, 0);
 
// 3 -- duplicate inputBuffer to outputBuffer
Byte[] outputBitmapData = inputBitmapData.Clone() as Byte[];
// 4 -- Calculate number of pixels to pass 
uint pixels = (uint)myBitmapFrame.Width * (uint)myBitmapFrame.Height;

// 5 -- Call the function 
cmsDoTransform(xform, inputBitmapData, outputBitmapData, pixels);