# File lib/puppet/util/windows/api_types.rb, line 61 def read_arbitrary_wide_string_up_to(max_char_length = 512) # max_char_length is number of wide chars (typically excluding NULLs), *not* bytes # use a pointer to read one UTF-16LE char (2 bytes) at a time wchar_ptr = FFI::Pointer.new(:wchar, address) # now iterate 2 bytes at a time until an offset lower than max_char_length is found 0.upto(max_char_length - 1) do |i| if wchar_ptr[i].read_wchar == NULL_TERMINATOR_WCHAR return read_wide_string(i) end end read_wide_string(max_char_length) end
# File lib/puppet/util/windows/api_types.rb, line 93 def read_com_memory_pointer(&block) ptr = nil begin ptr = read_pointer yield ptr ensure FFI::WIN32::CoTaskMemFree(ptr) if ptr && ! ptr.null? end # ptr has already had CoTaskMemFree called, so nothing to return nil end
# File lib/puppet/util/windows/api_types.rb, line 48 def read_handle type_size == 4 ? read_uint32 : read_uint64 end
# File lib/puppet/util/windows/api_types.rb, line 55 def read_wide_string(char_length) # 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(Encoding.default_external) end
# File lib/puppet/util/windows/api_types.rb, line 37 def read_win32_bool # BOOL is always a 32-bit integer in Win32 # some Win32 APIs return 1 for true, while others are non-0 read_int32 != FFI::WIN32_FALSE end
# File lib/puppet/util/windows/api_types.rb, line 76 def read_win32_local_pointer(&block) ptr = nil begin ptr = read_pointer yield ptr ensure if ptr && ! ptr.null? if FFI::WIN32::LocalFree(ptr.address) != FFI::Pointer::NULL_HANDLE Puppet.debug "LocalFree memory leak" end end end # ptr has already had LocalFree called, so nothing to return nil end
# File lib/puppet/util/windows/api_types.rb, line 24 def self.from_string_to_wide_string(str, &block) str = Puppet::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