Marcelo Zabani
Marcelo Zabani

Reputation: 2279

P/Invoke wrong parameters being passed to C library function

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

Once again, thanks in advance.

Upvotes: 2

Views: 506

Answers (1)

usr
usr

Reputation: 171178

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.

Upvotes: 2

Related Questions