[Win32utils-devel] FFI and wide character output buffers

Daniel Berger djberg96 at gmail.com
Thu Apr 19 21:23:34 UTC 2012


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


More information about the win32utils-devel mailing list