Module: Puppet::Util::Windows::COM::Factory

Defined in:
lib/puppet/util/windows/com.rb

Class Method Summary collapse

Class Method Details

.[](iface, clsid) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet/util/windows/com.rb', line 156

def self.[](iface, clsid)
  Class.new(iface) do
    send(:include, Helpers)

    const_set(:CLSID, clsid)

    def initialize(opts = {})
      @opts = opts

      @opts[:clsctx] ||= CLSCTX_INPROC_SERVER

      FFI::MemoryPointer.new(:pointer) do |ppv|
        hr = Puppet::Util::Windows::COM.CoCreateInstance(self.class::CLSID, FFI::Pointer::NULL, @opts[:clsctx], self.class::IID, ppv)
        if Puppet::Util::Windows::COM.FAILED(hr)
          raise _("CoCreateInstance failed (%{klass}).") % { klass: self.class }
        end

        self.pointer = ppv.read_pointer
      end

      @vtbl = self.class::VTBL.new(self[:lpVtbl])
    end

    attr_reader :vtbl

    self::VTBL.members.each do |name|
      define_method(name) do |*args|
        if Puppet::Util::Windows::COM.FAILED(result = @vtbl[name].call(self, *args))
          raise Puppet::Util::Windows::Error.new(_("Failed to call %{klass}::%{name} with HRESULT: %{result}.") % { klass: self, name: name, result: result }, result)
        end
        result
      end
    end

    layout \
      :lpVtbl, :pointer
  end
end