Module: WindowsCOM::COMCallback

Defined in:
lib/windows_com/common.rb

Class Method Summary collapse

Class Method Details

.[](iface) ⇒ Object



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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/windows_com/common.rb', line 228

def self.[](iface)
	Class.new(iface) {
		def initialize
			@vtbl = self.class::Vtbl.new
			@vtbl.members.each { |name|
				@vtbl[name] = instance_variable_set("@__ffi_func__#{name}",
					FFI::Function.new(*@vtbl.class::Spec[name].reverse, convention: :stdcall) { |*args|
						__send__(name, *args[1..-1]) # remove *this* pointer from Ruby meth call
					}
				)
			}

			@vptr = WindowsCOM::COMVptr_.new
			@vptr[:lpVtbl] = @vtbl

			@refc = 0
			AddRef()
		end

		attr_reader :refc

		def QueryInterface(iid, ppv)
			unless self.class::IID == iid || WindowsCOM::IUnknown::IID == iid
				STDERR.puts "#{self}.#{__method__} called with unsupported interface id (IID: #{iid})" if $DEBUG

				ppv.write_pointer(0)
				return WindowsCOM::E_NOINTERFACE
			end

			ppv.write_pointer(@vptr)
			AddRef()
			WindowsCOM::S_OK
		end

		def AddRef
			@refc += 1

			STDERR.puts "#{self}.#{__method__} (@refc: #{@refc})" if
				$DEBUG || WINDOWS_COM_TRACE_CALLBACK_REFCOUNT

			@refc
		end

		def Release
			@refc -= 1

			STDERR.puts "#{self}.#{__method__} (@refc: #{@refc})" if
				$DEBUG || WINDOWS_COM_TRACE_CALLBACK_REFCOUNT

			if @refc == 0
				@vtbl.pointer.free
				@vptr.pointer.free

				STDERR.puts "#{self} refcount is 0, #{@vtbl} and #{@vptr} freed" if
					$DEBUG || WINDOWS_COM_TRACE_CALLBACK_REFCOUNT
			end

			@refc
		end

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