Class: FFI::Pointer

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/util/windows/api_types.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_string_to_wide_string(str, &_block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pdk/util/windows/api_types.rb', line 6

def self.from_string_to_wide_string(str, &_block)
  str = PDK::Util::Windows::String.wide_string(str)
  FFI::MemoryPointer.new(:byte, str.bytesize) do |ptr|
    # uchar here is synonymous with byte
    ptr.put_array_of_uchar(0, str.bytes.to_a)

    yield ptr
  end

  # ptr has already had free called, so nothing to return
  nil
end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pdk/util/windows/api_types.rb', line 19

def read_wide_string(char_length, dst_encoding = Encoding::UTF_8, 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')
  str.encode(dst_encoding, str.encoding, encode_options)
rescue StandardError => e
  PDK.logger.debug _('Unable to convert value %{string} to encoding %{encoding} due to %{error}') % {
    string:   str.dump,
    encoding: dst_encoding,
    error:    e.inspect,
  }
  raise
end