Method: FFI::Pointer#read_wide_string

Defined in:
lib/puppet/ffi/windows/api_types.rb

#read_wide_string(char_length, dst_encoding = Encoding::UTF_8, strip = false, encode_options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/ffi/windows/api_types.rb', line 55

def read_wide_string(char_length, dst_encoding = Encoding::UTF_8, strip = false, encode_options = {})
  # char_length is number of wide chars (typically excluding NULLs), *not* bytes
  str = get_bytes(0, char_length * 2).force_encoding('UTF-16LE')

  if strip
    i = str.index(WCHAR_NULL)
    str = str[0, i] if i
  end

  str.encode(dst_encoding, str.encoding, **encode_options)
rescue EncodingError => e
  Puppet.debug { "Unable to convert value #{str.nil? ? 'nil' : str.dump} to encoding #{dst_encoding} due to #{e.inspect}" }
  raise
end