Subject: [ruby-ffi] Re: Unable to detect failure with reentrant function on Linux |
From: Daniel Berger |
Date: 6/14/10 10:26 AM |
To: ruby-ffi |
On Jun 12, 9:24 pm, Wayne Meissner <wmeiss...@gmail.com> wrote:
A couple of things: 1) You should map getprotobynumber_r as: attach_function 'getprotobynumber_r', [:int, :pointer, :pointer, :long, :pointer], :int 2) and call it like so: # getprotobynumber_r will use the buffer you pass in as the allocation area for the names # of the protocol, so allocate some native memory for it. namebuf = FFI::MemoryPointer.new(:char, 1024, false) pstruct = ProtocolStruct.new ptr = FFI::MemoryPointer.new(:pointer) rc = getprotobynumber_r(protocol, pstruct, namebuf, 1024, qptr) 3) a return val of 0 signifies success, and pstruct will be filled out with the data. For non-zero returns, you should probably check errno, and raise an exception if it is anything other than the not-found case. return rc == 0 && !qptr.get_pointer(0).null? ? pstruct[:p_name] : nil
Excellent, thanks! Dan