I have been trying to call the NewPixelRegionIterator function in ImageMagick’s library from C# code and have been running into a parameter passing problem.
More specifically, here is the definition of NewPixelRegionIterator:
PixelIterator NewPixelRegionIterator(MagickWand *wand,const ssize_t x,
const ssize_t y,const size_t width,const size_t height)
And here is the code that imports this function into C# code:
[DllImport("libMagickWandDev.so")]
internal static extern PixelIterator NewPixelRegionIterator(IntPtr MagickWand,
IntPtr left, IntPtr top, UIntPtr width, UIntPtr height);
The fact that I import libMagickWandDev.so and not libMagickWand.so is because libMagickWandDev.so is compiled with debugging support enabled, so that I could run mono with gdb to know if the problem was in the call to the C function (and in fact it was).
The problem, found through gdb, is that when calling NewPixelRegionIterator such as:
PixelIterator PixIt = PixelIteratorImports.NewPixelRegionIterator(MagickWandPtr,
new IntPtr(x), new IntPtr(y), new UIntPtr(1), new UIntPtr(1));
The actual parameters that are passed, found via gdb, are:
Breakpoint 1, NewPixelRegionIterator (wand=0x7fffffffc7a8, x=10744336, y=5,
width=6, height=1) at wand/pixel-iterator.c:418
- 5 and 6 are good, these are the actual numbers passed to the function (although x should be equal to 5 and y should equal 6, making width and height both equal to 1). This means that the order of the parameters is sort of shifted to the right, and that 0x7fffffffc7a8 is probably part of the struct to be returned or something else.
- If it helps, 10744336 is the value of MagickWandPtr (the address of the Magick Wand), because setting the wand parameter to this and x to a sane value while in gdb makes the function run just fine.
- PixelIterator is a struct that was mapped very carefully (I have also verified that the size of the struct through Marshal.SizeOf and actual C code’s sizeof() are the same 4192 bytes)
Once again, thanks in advance.
Are you sure you can just return a struct from a PInvoke-function? Don’t you need to return it using out parameters/pointers?
Because the parameters are shifted I’d expect a hidden parameter of type PixelIterator* as the first or the last parameter. And a return-type of void. This is how C compilers implement a struct-returning function under the hood.
In the comments you explained that returning a PixelIterator* solves the problem. The reason for that is probably that the C function is allocating an object and returning its pointer. The definition did not give a clue to that, though… Anyway, you probably need to free that memory when you are done with the object returned.