Daniel Berger
2012-Apr-19 16:54 UTC
[Win32utils-devel] FFI and wide character output buffers
Hi all,
I''ve seen how to deal with wide input strings with FFI, but
I''m not
sure how to deal with output buffers for wide character functions. I
tried this:
class Windows
extend FFI::Library
ffi_lib :kernel32
attach_function :GetTempPathA, [:ulong, :pointer], :long
attach_function :GetTempPathW, [:ulong, :pointer], :long
def self.temp_path
buf = 0.chr * 256
buf.encode!("UTF-16LE")
ptr = FFI::MemoryPointer.from_string(buf)
# segfault or interpreter failure here
if GetTempPathW(ptr.size, ptr) == 0
raise SystemCallError, FFI.errno, "GetTempPathW"
end
p ptr.read_string
end
end
Windows.temp_path
Any suggestions?
Regards,
Dan
Daniel Berger
2012-Apr-19 21:23 UTC
[Win32utils-devel] FFI and wide character output buffers
On Thu, Apr 19, 2012 at 12:54 PM, Daniel Berger <djberg96 at gmail.com> wrote:> Hi all, > > I''ve seen how to deal with wide input strings with FFI, but I''m not > sure how to deal with output buffers for wide character functions. I > tried this: > > class Windows > ?extend FFI::Library > > ?ffi_lib :kernel32 > > ?attach_function :GetTempPathA, [:ulong, :pointer], :long > ?attach_function :GetTempPathW, [:ulong, :pointer], :long > > ?def self.temp_path > ? ?buf = 0.chr * 256 > ? ?buf.encode!("UTF-16LE") > ? ?ptr = FFI::MemoryPointer.from_string(buf) > > ? ?# segfault or interpreter failure here > ? ?if GetTempPathW(ptr.size, ptr) == 0 > ? ? ?raise SystemCallError, FFI.errno, "GetTempPathW" > ? ?end > > ? ?p ptr.read_string > ?end > end > > Windows.temp_path > > Any suggestions?Disregard. Changing the second arg from :pointer to :buffer_out and just using an encoded string worked. Regards, Dan