Module: Unknownr::Windows::COM::Factory

Defined in:
lib/unknownr.rb

Class Method Summary collapse

Class Method Details

.[](iface, clsid) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/unknownr.rb', line 181

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

		const_set(:CLSID, clsid)

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

			@opts[:clsctx] ||= CLSCTX_INPROC_SERVER

			FFI::MemoryPointer.new(:pointer) { |ppv|
				raise "CoCreateInstance failed (#{self.class})." if
					Windows.FAILED(Windows.CoCreateInstance(self.class::CLSID, nil, @opts[:clsctx], self.class::IID, ppv))

				self.pointer = ppv.read_pointer
			}

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

		attr_reader :vtbl

		self::VTBL.members.each { |name|
			define_method(name) { |*args|
				raise "#{self}.#{name} failed." if Windows.FAILED(result = @vtbl[name].call(self, *args)); result
			}
		}
	}
end