[Win32utils-devel] FFI and wide character output buffers

Daniel Berger djberg96 at gmail.com
Thu Apr 19 16:54:11 UTC 2012


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


More information about the win32utils-devel mailing list