Sha256: 1356bf5b5b5e2e4f2a0e2c55185e2a4ac9fa5a5e5d35fd46141cbfc4ff65c0bc

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require 'facter/util/windows'
require 'ffi'

module Facter::Util::Windows::Dir
  extend FFI::Library

  COMMON_APPDATA = 0x0023
  S_OK           = 0x0
  MAX_PATH       = 260;

  def get_common_appdata
    common_appdata = ''

    # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us
    FFI::MemoryPointer.new(:pointer, ((MAX_PATH + 1) * 2)) do |buffer_ptr|
      # hwndOwner, nFolder, hToken, dwFlags, pszPath
      if SHGetFolderPathW(0, COMMON_APPDATA, 0, 0, buffer_ptr) != S_OK
        raise Facter::Util::Windows::Error.new("Could not find COMMON_APPDATA path")
      end

      common_appdata = buffer_ptr.read_arbitrary_wide_string_up_to(MAX_PATH + 1)
    end

    common_appdata
  end
  module_function :get_common_appdata

  ffi_convention :stdcall

  # https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx
  # HRESULT SHGetFolderPath(
  #   _In_  HWND   hwndOwner,
  #   _In_  int    nFolder,
  #   _In_  HANDLE hToken,
  #   _In_  DWORD  dwFlags,
  #   _Out_ LPTSTR pszPath
  # );
  ffi_lib :shell32
  attach_function_private :SHGetFolderPathW,
    [:handle, :int32, :handle, :dword, :lpwstr], :hresult
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facter-2.5.0 lib/facter/util/windows/dir.rb
facter-2.5.0-x86-mingw32 lib/facter/util/windows/dir.rb
facter-2.5.0-x64-mingw32 lib/facter/util/windows/dir.rb
facter-2.5.0-universal-darwin lib/facter/util/windows/dir.rb