Module: Fzeet::Windows::COM::Callback

Defined in:
lib/fzeet/windows/com/Common.rb

Class Method Summary collapse

Class Method Details

.[](iface) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/fzeet/windows/com/Common.rb', line 213

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

    def initialize(opts = {})
      @vtbl, @refc = self.class::VTBL.new, 1

      @vtbl.members.each { |name|
        @vtbl[name] = instance_variable_set("@fn#{name}",
          FFI::Function.new(*@vtbl.class::SPEC[name].reverse, convention: :stdcall) { |*args|
            send(name, *args[1..-1])
          }
        )
      }

      self[:lpVtbl] = @vtbl

      begin
        yield self
      ensure
        Release()
      end if block_given?
    end

    attr_reader :vtbl, :refc

    def QueryInterface(riid, ppv)
      if [IUnknown::IID, self.class::IID].any? { |iid| Windows.memcmp(riid, iid, iid.size) == 0 }
        ppv.write_pointer(self)
      else
        ppv.write_pointer(0); return E_NOINTERFACE
      end

      AddRef(); S_OK
    end

    def AddRef
      @refc += 1
    end

    def Release
      @refc -= 1
    end

    (self::VTBL.members - IUnknown::VTBL.members).each { |name|
      define_method(name) { |*args|
        E_NOTIMPL
      }
    }
  }
end