Subject:
Re: [ruby-ffi] How to specify size for returned buffer?
From:
Matijs van Zuijlen
Date:
8/21/11 12:58 PM
To:
ruby-ffi@googlegroups.com

Hi cfis,

On 08/20/2011 06:57 AM, cfis wrote:
I'm trying to wrap FreeImage (http://freeimage.sourceforge.net/) and
it has this function:

DLL_API BOOL DLL_CALLCONV FreeImage_AcquireMemory(FIMEMORY *stream,
BYTE **data, DWORD *size_in_bytes);

The way this works is you hand FreeImage a pointer (data) which it
then allocates memory for and returns the size in the size_in_bytes
out parameter.  I've attached example C code from the FreeImage docs
in case it helps.

So I'm not sure how to wrap this function.  So far I have this:

FreeImage.attach_function('FreeImage_AcquireMemory',
[:pointer, :pointer, :uint], :bool)

Since all the arguments in C are pointers, the argument specification should read [:pointer, :pointer, :pointer].

buffer = MemoryPointer.new(:pointer)
size_ptr = MemoryPointer.new(:uint)
FreeImage.FreeImage_AcquireMemory(self, buffer, size);

The call would then be

  FreeImage.FreeImage_AcquireMemory(self, buffer, size_ptr)

But perhaps you were doing that already and didn't copy and paste properly?

What I would then like to do is something like:

buffer.size = size_ptr.read_pointer()

But buffer doesn't have any such API.

There really is no need to do that. Besides, the call would be size_ptr.read_uint.

What you want to do is:

  size = size_ptr.read_uint
  data_ptr = buffer.read_pointer

And then you can read data using:

  data_ptr.get_uint8(offset)

Making sure that offset < size.

Any help appreciated.

Thanks,

Charlie

HTH,
-- 
Matijs