Class: WIN32OLE

Inherits:
Object
  • Object
show all
Defined in:
lib/win32ole-pr.rb

Defined Under Namespace

Modules: VARIANT Classes: ComVtbl, Win32OLEIDispatch

Constant Summary collapse

VERSION =
"1.3.9"
ARGV =
[]
CP_ACP =
Windows::Unicode::CP_ACP
CP_OEMCP =
Windows::Unicode::CP_OEMCP
CP_MACCP =
Windows::Unicode::CP_MACCP
CP_THREAD_ACP =
Windows::Unicode::CP_THREAD_ACP
CP_SYMBOL =
Windows::Unicode::CP_SYMBOL
CP_UTF7 =
Windows::Unicode::CP_UTF7
CP_UTF8 =
Windows::Unicode::CP_UTF8
LOCALE_SYSTEM_DEFAULT =
Windows::National::LOCALE_SYSTEM_DEFAULT
LOCALE_USER_DEFAULT =
Windows::National::LOCALE_USER_DEFAULT
LCID_INSTALLED =
0x00000001
CP_INSTALLED =
0x00000001
IID_IDispatch =
[0x00020400, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46].pack('LSSC8')
IID_IUnknown =
[0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46].pack('LSSC8')
IID_NULL =
[0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00].pack('LSSC8')
IID_IEnumVARIANT =
[0x00020404, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46].pack('LSSC8')
IID_IPersistMemory =
[0xBD1AE5E0, 0xA6AE, 0xBD37, 0xBD, 0x37, 0x50, 0x42, 0x00, 0xC1, 0x00, 0x00].pack('LSSC8')
IID_IConnectionPointContainer =
[0xB196B284, 0xBAB4, 0x101A, 0xB6, 0x9C, 0x00, 0xAA, 0x00, 0x34, 0x1D, 0x07].pack('LSSC8')
IID_IProvideClassInfo =
[0xB196B283, 0xBAB4, 0x101A, 0xB6, 0x9C, 0x00, 0xAA, 0x00, 0x34, 0x1D, 0x07].pack('LSSC8')
IID_IProvideClassInfo2 =
[0xA6BC3AC0, 0xDBAA, 0x11CE, 0x9D, 0xE3, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51].pack('LSSC8')
@@ole_initialized =
false
@@lcid =
LOCALE_SYSTEM_DEFAULT
@@cp =
CP_ACP
@@nil_to =
VT_ERROR
@@com_hash =
{}
@@QueryInterface =
Win32::API::Callback.new('PPP', 'L', &lambda { |p, riid, ppv|
  if riid == IID_IUnknown || riid == IID_IDispatch
    refcount = 0.chr * 4
    memcpy(refcount, p+4, 4)
    refcount = [refcount.unpack('L').first + 1].pack('L')
    memcpy(p+4, refcount, 4)
    ppv[0, 4] = [p].pack('L')
    return S_OK
  end
  E_NOINTERFACE
})
@@AddRef =
Win32::API::Callback.new('P', 'L') { |p|
  refcount = 0.chr * 4
  memcpy(refcount, p+4, 4)
  refcount = [refcount.unpack('L').first + 1].pack('L')
  memcpy(p+4, refcount, 4)
}
@@Release =
Win32::API::Callback.new('P', 'L') { |p|
  dispatch = 0.chr * 12
  memcpy(dispatch, p, 12)
  _, refcount, obj = dispatch.unpack('L*')
  refcount -= 1
  memcpy(p+4, [refcount].pack('L'), 4)
  u = refcount
  if u == 0
    key = ObjectSpace._id2ref(obj)
    @@com_hash.delete(key)
  end
  u
}
@@Invoke =
Win32::API::Callback.new('PLPLLPPPP', 'L') { |p, dispid, riid, lcid, wFlags, pdispparams, pvarResult, pexceptinfo, puArgErr|
  obj = 0.chr * 4
  memcpy(obj, p+8, 4)
  obj = ObjectSpace._id2ref(obj.unpack('L').first)
  dispparam = 0.chr * 16
  memcpy(dispparam, pdispparams, 16)
  args = dispparam[8, 4].unpack('L').first
  parg = []
  for i in 0 ... args
    var = 0.chr * 16
    memcpy(var, dispparam[0, 4].unpack('L').first + (args-i-1)*16, 16)
    parg[i] = ole_variant2val(var)
  end
  if dispid == DISPID_VALUE
    if wFlags == DISPATCH_METHOD
      dispid = :call
    elsif (wFlags & DISPATCH_PROPERTYGET) != 0
      dispid = :value
    end
  else
    dispid = ObjectSpace._id2ref(dispid)
  end
  v = obj.send(dispid, *parg)
  p = 0.chr * 16
  WIN32OLE.ole_val2variant(v, p)
  pvarResult[0, 16] = p
  S_OK
}
@@GetIDsOfNames =
Win32::API::Callback.new('PPLLLP', 'L') { |p, riid, sznames, cNames, lcid, pDispID|
  str = 0.chr * 256
  wcscpy(str, sznames)
  pDispID[0, 4] = [wide_to_mult(str).to_sym.object_id].pack('L')
  S_OK
}
@@GetTypeInfoCount =
Win32::API::Callback.new('PP', 'L') { |p, pct|
  E_NOTIMPL
}
@@GetTypeInfo =
Win32::API::Callback.new('PLLP', 'L') { |p, info, lcid, pInfo|
  E_NOTIMPL
}
@@installed_lcid_proc =
Win32::API::Callback.new('L', 'L', &lambda { |ptr|
  str = 0.chr * 8
  strcpy(str, ptr)
  if str == @lcid
    @installed = true
    return 0
  end
  1
})
@@installed_code_page_proc =
Win32::API::Callback.new('L', 'L', &lambda { |ptr|
  str = 0.chr * 10
  strcpy(str, ptr)
  if str.to_i == @cp
    @installed = true
    return 0
  end
  1
})
@@com_vtbl =
ComVtbl.new(@@QueryInterface, @@AddRef, @@Release, @@GetTypeInfoCount, @@GetTypeInfo, @@GetIDsOfNames, @@Invoke).to_a.map { |x| x.address }.pack('L*')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = nil, host = nil) ⇒ WIN32OLE

Creates a new Win32::OLE server object on host, or the localhost if no host is specified. The server can be either a Program ID or a Class ID.

Examples:

# Program ID (Excel)
ole = Win32::OLE.new('Excel.Application')

# Class ID (Excel)
ole = Win32::OLE.new('{00024500-0000-0000-C000-000000000046}')

– TODO: explain numeric server and host behavior.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/win32ole-pr.rb', line 309

def initialize(server=nil, host=nil)
  WIN32OLE.ole_initialize()

  @pDispatch = nil

  return self if server.nil?

  if server.is_a?(Numeric)
    ole_set_member(server)
    return self
  end

  if host
    return ole_create_dcom(server, host)
  end

  clsid = 0.chr * 16
  serverw = multi_to_wide(server)

  hr = CLSIDFromProgID(serverw, clsid)

  if hr != S_OK
    hr = CLSIDFromString(serverw, clsid)
  end

  if hr != S_OK
    raise WIN32OLERuntimeError, "unknown OLE server: '#{server}'"
  end

  ptr = 0.chr * 4

  hr = CoCreateInstance(
      clsid,
      nil,
      CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
      IID_IDispatch,
      ptr
  )

  if hr != S_OK
    error = "failed to create WIN32OLE object from '#{server}'"
    raise WIN32OLERuntimeError, error
  end

  ole_set_member(ptr.unpack('L').first)

  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



1903
1904
1905
1906
1907
1908
1909
1910
1911
# File 'lib/win32ole-pr.rb', line 1903

def method_missing(id, *args)
  method = id.to_s
  if method[-1] == ?=
    method = method[0..-2]
    return ole_propertyput(method, args.first)
  else
    return ole_invoke(method, args, DISPATCH_METHOD|DISPATCH_PROPERTYGET, false)
  end
end

Instance Attribute Details

#pDispatchObject

Returns the value of attribute pDispatch.



161
162
163
# File 'lib/win32ole-pr.rb', line 161

def pDispatch
  @pDispatch
end

Class Method Details

._ole_free(dispatch) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/win32ole-pr.rb', line 187

def self._ole_free(dispatch)
  if @@ole_initialized
    if dispatch
      WIN32OLE.ole_release(dispatch)
    end
  end
  nil
end

.ary_len_of_dim(ary, dim) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/win32ole-pr.rb', line 524

def self.ary_len_of_dim(ary, dim)
  ary_len = 0
  if dim == 0
    if ary.is_a?(Array)
      ary_len = ary.length
    end
  else
    if ary.is_a?(Array)
      ary.each do |x|
        ary_len1 = ary_len_of_dim(x, dim-1)
        ary_len = ary_len1 if ary_len < ary_len1
      end
    end
  end
  ary_len
end

.ary_new_dim(myary, pid, plb, dim) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/win32ole-pr.rb', line 439

def self.ary_new_dim(myary, pid, plb, dim)
  ids = Array.new(dim)
  for i in 0 ... dim
    ids[i] = pid[i] - plb[i]
  end
  obj = myary
  pobj = myary
  for i in 0 ... (dim-1)
    obj = pobj[ids[i]]
    if obj.nil?
      pboj[ids[i]] = []
    end
    obj = pobj[ids[i]]
    pobj = pbj
  end
  obj
end

.ary_store_dim(myary, pid, plb, dim, val) ⇒ Object



457
458
459
460
461
# File 'lib/win32ole-pr.rb', line 457

def self.ary_store_dim(myary, pid, plb, dim, val)
  id = pid[dim-1] - plb[dim-1]
  obj = ary_new_dim(myary, pid, plb, dim)
  obj[id] = val
end

.attach(dispatch) ⇒ Object



291
292
293
# File 'lib/win32ole-pr.rb', line 291

def self.attach(dispatch)
  obj = self.new.ole_set_member(dispatch)
end

.codepageObject

Returns the current code page.

Example:

WIN32OLE.codepage # => WIN32OLE::CP_ACP


1292
1293
1294
# File 'lib/win32ole-pr.rb', line 1292

def self.codepage
  @@cp
end

.codepage=(cp) ⇒ Object

Sets the current code page. The code page must be an installed code page or one of the following values:

WIN32OLE::CP_ACP WIN32OLE::CP_OEMCP WIN32OLE::CP_MACCP WIN32OLE::CP_THREAD_ACP WIN32OLE::CP_SYMBOL WIN32OLE::CP_UTF7 WIN32OLE::CP_UTF8

Example:

WIN32OLE.codepage = WIN32OLE::CP_UTF8

– TODO: What is load_conv_function51932?



1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/win32ole-pr.rb', line 1318

def self.codepage=(cp)
  if code_page_installed(cp)
    @@cp = cp
  else
    case cp
      when CP_ACP, CP_OEMCP, CP_MACCP, CP_THREAD_ACP,
          CP_SYMBOL, CP_UTF7, CP_UTF8, 51932
      then
        @@cp = cp
        if cp == 51932
          load_conv_function51932()
        end
      else
        msg = "codepage should be WIN32OLE::CP_ACP, WIN32OLE::CP_OEMCP, "
        msg << "WIN32OLE::CP_MACCP, WIN32OLE::CP_THREAD_ACP, "
        msg << "WIN32OLE::CP_SYMBOL, WIN32OLE::CP_UTF7, WIN32OLE::CP_UTF8"
        raise WIN32OLERuntimeError, msg
    end
  end
  @@enc = ole_cp2encoding(@@cp)
  nil
end

.connect(server) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/win32ole-pr.rb', line 404

def self.connect(server)
  ole_initialize()
  clsid = 0.chr * 16
  serverw = Windows::Unicode::multi_to_wide(server)
  hr = CLSIDFromProgID(serverw, clsid)
  if hr != S_OK
    hr = CLSIDFromString(serverw, clsid)
  end
  if hr != S_OK
    return ole_bind_obj(server)
  end

  pUnknown = 0.chr * 4
  hr = GetActiveObject(clsid, nil, pUnknown)
  if hr != S_OK
    raise WIN32OLERuntimeError, "OLE server `#{server}' not running"
  end
  pUnknown = pUnknown.unpack('L').first
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, pUnknown, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  queryInterface = Win32::API::Function.new(table[0], 'PPP', 'L')
  p = 0.chr * 4
  hr = queryInterface.call(pUnknown, IID_IDispatch, p)
  if hr != S_OK
    WIN32OLE.ole_release(pUnknown)
    raise WIN32OLERuntimeError, "failed to create WIN32OLE server `#{server}'"
  end
  pDispatch = p.unpack('L').first
  WIN32OLE.ole_release(pUnknown)
  self.attach(pDispatch)
end

.const_load(ole, mod = WIN32OLE) ⇒ Object

Defines a constant of an OLE Automation server as a mod constant.

The +ole+ argument is WIN32OLE object or type library name. The
+mod+ argument is a class or module name, which defaults to WIN32OLE
if omitted.

The first letter of a Ruby constant is always upper case. Therefore,
a constant name of WIN32OLE object is capitalized. For example, the
'xlTop' constant of Excel is changed to 'XlTop' in WIN32OLE.

If the first letter of an OLE constant is not in the A-Z range, then
the constant must be referenced via the CONSTANTS hash variable.

Example:

   module EXCEL_CONST; end

   excel = WIN32OLE.new('Excel.Application')

   WIN32OLE.const_load(excel, EXCEL_CONST)

   puts EXCEL_CONST::XlTop # => -4160
   puts EXCEL_CONST::CONSTANTS['_xlDialogChartSourceData'] # => 541

   WIN32OLE.const_load(excel)
   puts WIN32OLE::XlTop # => -4160

   module MSO; end

   WIN32OLE.const_load('Microsoft Office 9.0 Object Library', MSO)
   puts MSO::MsoLineSingle # => 1


1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/win32ole-pr.rb', line 1133

def self.const_load(ole, mod=WIN32OLE)
  unless [Class, Module].include?(mod.class)
    raise TypeError, '2nd parameter must be Class or Module'
  end

  if ole.is_a?(WIN32OLE)
    pDispatch = ole.pDispatch
    lpVtbl = 0.chr * 4
    table = 0.chr * 28

    memcpy(lpVtbl, pDispatch, 4)
    memcpy(table, lpVtbl.unpack('L').first, 28)

    table = table.unpack('L*')

    ptr = 0.chr * 4

    getTypeInfo = Win32::API::Function.new(table[4], 'PLLP', 'L')
    hr = getTypeInfo.call(pDispatch, 0, @@lcid, ptr)

    if hr != S_OK
      raise RuntimeError, 'failed to GetTypeInfo'
    end

    pTypeInfo = ptr.unpack('L').first
    lpVtbl = 0.chr * 4
    table = 0.chr * 88

    memcpy(lpVtbl, pTypeInfo, 4)
    memcpy(table, lpVtbl.unpack('L').first, 88)

    table = table.unpack('L*')
    getContainingTypeLib = Win32::API::Function.new(table[18], 'PPP', 'L')

    ptr = 0.chr * 4
    index = 0.chr * 4
    hr = getContainingTypeLib.call(pTypeInfo, ptr, index)

    if hr != S_OK
      WIN32OLE.ole_release(pTypeInfo)
      raise RuntimeError, 'failed to GetContainingTypeLib'
    end

    pTypeLib = ptr.unpack('L').first
    WIN32OLE.ole_release(pTypeInfo)
    ole_const_load(pTypeLib, mod)
    WIN32OLE.ole_release(pTypeLib)
  elsif ole.is_a?(String)
    file = WIN32OLE_TYPELIB.typelib_file(ole)
    file = ole if file.nil?

    buf = multi_to_wide(file)
    p = 0.chr * 4

    hr = LoadTypeLibEx.call(buf, REGKIND_NONE, p)
    pTypeLib = p.unpack('L').first

    if hr != S_OK
      raise WIN32OLERuntimeError, "failed to LoadTypeLibEx"
    end

    if mod
      ole_const_load(pTypeLib, mod)
    else
      ole_const_load(pTypeLib, WIN32OLE)
    end

    WIN32OLE.ole_release(pTypeLib)
  else
    raise TypeError, "1st parameter must be WIN32OLE instance"
  end

  nil
end

.create_guidObject



1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/win32ole-pr.rb', line 1366

def self.create_guid
  guid = 0.chr * 16
  hr = CoCreateGuid(guid)
  if hr != S_OK
    raise = WIN32OLERuntimeError, "failed to create GUID"
  end
  bstr = 0.chr * 160
  len = StringFromGUID2(guid, bstr, 80)
  if len == 0
    raise RuntimeError, "failed to create GUID(buffer over)"
  end
  wide_to_multi(bstr)
end

.dimension(val) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
# File 'lib/win32ole-pr.rb', line 512

def self.dimension(val)
  dim = 0
  if val.is_a?(Array)
    val.each do |x|
      dim1 = dimension(x)
      dim = dim1 if dim < dim1
    end
    dim += 1
  end
  dim
end

.get_control_from_hwnd(hnd) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/win32ole-pr.rb', line 270

def self.get_control_from_hwnd(hnd)
  CoInitialize(0)
  reg_msg = RegisterWindowMessage("WM_HTML_GETOBJECT")

  iid =[0x626FC520, 0xA41E, 0x11CF, 0xA7, 0x31, 0x00, 0xA0, 0xC9, 0x08, 0x26, 0x37].pack('LSSC8')

  result = 0.chr*4
  hnd = hnd.hex if hnd.is_a?(String)
  SendMessageTimeout(hnd, reg_msg, 0, 0, SMTO_ABORTIFHUNG, 1000, result)

  result = result.unpack('L')[0]
  idisp = 0.chr * 4
  r = ObjectFromLresult(result, iid, 0, idisp)
  if r == 0
    idisp = idisp.unpack('L').first
    self.attach(idisp)
  else
    nil
  end
end

.get_ptr_of_variant(pvar) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
# File 'lib/win32ole-pr.rb', line 551

def self.get_ptr_of_variant(pvar)
  case pvar[0, 2].unpack('S').first
    when VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_R4, VT_R8, VT_I8, VT_UI8, VT_INT, VT_UINT, VT_CY, VT_DATE,
        VT_ERROR, VT_BOOK, VT_ARRAY
      return [pvar].pack('P').unpack('L').first + 8
    when VT_BSTR, VT_DISPATCH, VT_UNKNOWN
      return pvar[8, 4].unpack('L').first
    else
      return 0
  end
end

.is_all_index_under(pid, pub, dim) ⇒ Object



583
584
585
586
587
588
# File 'lib/win32ole-pr.rb', line 583

def self.is_all_index_under(pid, pub, dim)
  for i in 0 ... dim
    return false if pid[i] > pub[i]
  end
  true
end

.lcid_installed(lcid) ⇒ Object



1345
1346
1347
1348
1349
1350
# File 'lib/win32ole-pr.rb', line 1345

def self.lcid_installed(lcid)
  @installed = false
  @lcid = "%08x" % lcid
  EnumSystemLocales(@@installed_lcid_proc, LCID_INSTALLED)
  @installed
end

.localeObject



1341
1342
1343
# File 'lib/win32ole-pr.rb', line 1341

def self.locale
  @@lcid
end

.locale=(lcid) ⇒ Object



1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/win32ole-pr.rb', line 1352

def self.locale=(lcid)
  if lcid_installed(lcid)
    @@lcid = lcid
  else
    case lcid
      when LOCALE_SYSTEM_DEFAULT, LOCALE_USER_DEFAULT
        @@lcid = lcid
      else
        raise WIN32OLERuntimeError, "not installed locale: #{lcid}"
    end
  end
  nil
end

.ole_addref(dispatch) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/win32ole-pr.rb', line 206

def self.ole_addref(dispatch)
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, dispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  addRef = Win32::API::Function.new(table[1], 'P', 'L')
  addRef.call(dispatch)
end

.ole_ary_m_entry(val, pid) ⇒ Object



541
542
543
544
545
546
547
548
549
# File 'lib/win32ole-pr.rb', line 541

def self.ole_ary_m_entry(val, pid)
  obj = val
  i = 0
  while obj.is_a?(Array)
    obj = obj[pid[i]]
    i += 1
  end
  obj
end

.ole_bind_obj(moniker) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/win32ole-pr.rb', line 358

def self.ole_bind_obj(moniker)
  pBindCtx = 0.chr * 4
  hr = CreateBindCtx(0, pBindCtx)

  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to create bind context"
  end

  pBindCtx = pBindCtx.unpack('L').first
  monikerw = multi_to_wide(moniker)

  pMoniker = 0.chr * 4
  eaten = 0.chr * 4

  hr = MkParseDisplayName(pBindCtx, monikerw, eaten, pMoniker)

  if hr != S_OK
    WIN32OLE.ole_release(pBindCtx)
    raise WIN32OLERuntimeError, "failed to parse display name of moniker '#{moniker}'"
  end

  pMoniker = pMoniker.unpack('L').first
  lpVtbl = 0.chr * 4
  table = 0.chr * 4 * 23

  memcpy(lpVtbl, pMoniker, 4)
  memcpy(table, lpVtbl.unpack('L').first, 92)

  table = table.unpack('L*')
  ptr = 0.chr * 4

  bindToObject = Win32::API::Function.new(table[8], 'PPPPP', 'L')

  hr = bindToObject.call(pMoniker, pBindCtx, nil, IID_IDispatch, ptr)

  pDispatch = ptr.unpack('L').first
  WIN32OLE.ole_release(pMoniker)
  WIN32OLE.ole_release(pBindCtx)

  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to bind moniker '#{moniker}'"
  end

  self.attach(pDispatch)
end

.ole_const_load(pTypeLib, mod) ⇒ Object



1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/win32ole-pr.rb', line 1028

def self.ole_const_load(pTypeLib, mod)
  constant = {}
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, pTypeLib, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  getTypeInfoCount = Win32::API::Function.new(table[3], 'P', 'L')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLP', 'L')
  count = getTypeInfoCount.call(pTypeLib)
  for index in 0 ... count
    p = 0.chr * 4
    hr = getTypeInfo.call(pTypeLib, index, p)
    next if hr != S_OK
    pTypeInfo = p.unpack('L').first
    lpVtbl = 0.chr * 4
    table = 0.chr * 88
    memcpy(lpVtbl, pTypeInfo, 4)
    memcpy(table, lpVtbl.unpack('L').first, 88)
    table = table.unpack('L*')
    getTypeAttr = Win32::API::Function.new(table[3], 'PP', 'L')
    getVarDesc = Win32::API::Function.new(table[6], 'PLP', 'L')
    getNames = Win32::API::Function.new(table[7], 'PLPLP', 'L')
    releaseTypeAttr = Win32::API::Function.new(table[19], 'PP', 'L')
    releaseVarDesc = Win32::API::Function.new(table[21], 'PP', 'L')
    p = 0.chr * 4
    hr = getTypeAttr.call(pTypeInfo, p)
    if hr != S_OK
      WIN32OLE.ole_release(pTypeInfo)
      next
    end
    pTypeAttr = p.unpack('L').first
    typeAttr = 0.chr * 76
    memcpy(typeAttr, pTypeAttr, 76)
    for iVar in 0 ... typeAttr[46, 2].unpack('S').first
      p = 0.chr * 4
      getVarDesc.call(pTypeInfo, iVar, p)
      pVarDesc = p.unpack('L').first
      next if hr != S_OK
      varDesc = 0.chr * 36
      memcpy(varDesc, pVarDesc, 36)
      if varDesc[32, 4].unpack('L').first == VAR_CONST &&
          (varDesc[28, 2].unpack('S').first & (VARFLAG_FHIDDEN |
              VARFLAG_FRESTRICTED | VARFLAG_FNONBROWSABLE))==0
        len = 0.chr * 4
        bstr = 0.chr * 4
        hr = getNames.call(pTypeInfo, varDesc[0, 4].unpack('L').first, bstr, 1, len)
        if hr != S_OK || len.unpack('L').first == 0 ||
            bstr.unpack('L').first == 0
          next
        end
        bstr = bstr.unpack('L').first
        str = 0.chr * 256
        wcscpy(str, bstr)
        SysFreeString(bstr)
        pName = wide_to_multi(str)
        pName[0, 1] = pName[0, 1].upcase
        var = 0.chr * 16
        memcpy(var, varDesc[8, 4].unpack('L').first, 16)
        val = ole_variant2val(var)
        if pName[0, 1].between?('A', 'Z')
          mod.const_set(pName.to_sym, val)
        else
          constant[pName] = val
        end
      end
      releaseVarDesc.call(pTypeInfo, pVarDesc)
    end
    releaseTypeAttr.call(pTypeInfo, pTypeAttr)
    WIN32OLE.ole_release(pTypeInfo)
  end
  mod.const_set("CONSTANTS", constant)
end

.ole_cp2encoding(cp) ⇒ Object



1296
1297
1298
1299
# File 'lib/win32ole-pr.rb', line 1296

def self.ole_cp2encoding(cp)
  # TODO
  nil
end

.ole_free(obj) ⇒ Object

Invokes the Release() method of the Dispatch interface of the WIN32OLE object. It returns the reference counter of the OLE object.

This method is used for debugging only.



1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/win32ole-pr.rb', line 1213

def self.ole_free(obj)
  if obj.pDispatch
    if ole_reference_count(obj) > 0
      n = WIN32OLE.ole_release(obj.pDispatch)
    end
  end

  n
end

.ole_initializeObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/win32ole-pr.rb', line 168

def self.ole_initialize
  if !@@ole_initialized
    hr = OleInitialize.call()
    if hr != S_OK
      raise RuntimeError, "fail: OLE initialize"
    end
    @@ole_initialized = true
    at_exit { ole_uninitialize }
  end
end

.ole_methods_from_typeinfo(pTypeInfo, mask) ⇒ Object



2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
# File 'lib/win32ole-pr.rb', line 2027

def self.ole_methods_from_typeinfo(pTypeInfo, mask)
  methods = []
  lpVtbl = 0.chr * 4
  table = 0.chr * 80
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 80)
  table = table.unpack('L*')
  getTypeAttr = Win32::API::Function.new(table[3], 'PP', 'L')
  getRefTypeOfImplType = Win32::API::Function.new(table[8], 'PLP', 'L')
  getRefTypeInfo = Win32::API::Function.new(table[14], 'PLP', 'L')
  getFuncDesc = Win32::API::Function.new(table[5], 'PLP', 'L')
  getDocumentation = Win32::API::Function.new(table[12], 'PLPPPP', 'L')
  releaseTypeAttr = Win32::API::Function.new(table[19], 'PP', 'L')
  p = 0.chr * 4
  hr = getTypeAttr.call(pTypeInfo, p)
  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to GetTypeAttr"
  end
  ole_methods_sub(0, pTypeInfo, methods, mask)
  pTypeAttr = p.unpack('L').first
  typeAttr = 0.chr * 76
  memcpy(typeAttr, pTypeAttr, 76)
  for i in 0 ... typeAttr[48, 2].unpack('S').first
    p = 0.chr * 4
    hr = getRefTypeOfImplType.call(pTypeInfo, i, p)
    next if hr != S_OK
    href = p.unpack('L').first
    p = 0.chr * 4
    hr = getRefTypeInfo.call(pTypeInfo, href, p)
    next if hr != S_OK
    pRefTypeInfo = p.unpack('L').first
    ole_methods_sub(pTypeInfo, pRefTypeInfo, methods, mask)
    ole_release(pRefTypeInfo)
  end
  releaseTypeAttr.call(pTypeInfo, pTypeAttr)
  methods
end

.ole_methods_sub(pOwnerTypeInfo, pTypeInfo, methods, mask) ⇒ Object



1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
# File 'lib/win32ole-pr.rb', line 1979

def self.ole_methods_sub(pOwnerTypeInfo, pTypeInfo, methods, mask)
  lpVtbl = 0.chr * 4
  table = 0.chr * 88
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 88)
  table = table.unpack('L*')
  getTypeAttr = Win32::API::Function.new(table[3], 'PP', 'L')
  getFuncDesc = Win32::API::Function.new(table[5], 'PLP', 'L')
  getNames = Win32::API::Function.new(table[7], 'PLPLP', 'L')
  getDocumentation = Win32::API::Function.new(table[12], 'PLPPPP', 'L')
  releaseTypeAttr = Win32::API::Function.new(table[19], 'PP', 'L')
  releaseFuncDesc = Win32::API::Function.new(table[20], 'PP', 'L')
  p = 0.chr * 4
  hr = getTypeAttr.call(pTypeInfo, p)
  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to GetTypeAttr"
  end
  pTypeAttr = p.unpack('L').first
  typeAttr = 0.chr * 76
  memcpy(typeAttr, pTypeAttr, 76)
  for i in 0 .. typeAttr[44, 2].unpack('S').first
    p = 0.chr * 4
    hr = getFuncDesc.call(pTypeInfo, i, p)
    next if hr != S_OK
    pFuncDesc = p.unpack('L').first
    funcDesc = 0.chr * 52
    memcpy(funcDesc, pFuncDesc, 52)
    memid = funcDesc[0, 4].unpack('L').first
    bstr = 0.chr * 4
    hr = getDocumentation.call(pTypeInfo, memid, bstr, nil, nil, nil)
    if hr != S_OK
      releaseFuncDesc.call(pTypeInfo, pFuncDesc)
      next
    end
    if (funcDesc[16, 4].unpack('L').first & mask) != 0
      str = 0.chr * 256
      wcscpy(str, bstr.unpack('L').first)
      method = WIN32OLE_METHOD.new
      method.olemethod_set_member(pTypeInfo, pOwnerTypeInfo, i, wide_to_multi(str))
      methods.push(method)
    end
    releaseFuncDesc.call(pTypeInfo, pFuncDesc)
    pFuncDesc = nil
  end
  releaseTypeAttr.call(pTypeInfo, pTypeAttr)
  methods
end

.ole_reference_count(obj) ⇒ Object

Returns the reference counter of the Dispatch interface of the WIN32OLE object.

This method is used for debugging only.



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
# File 'lib/win32ole-pr.rb', line 1228

def self.ole_reference_count(obj)
  n = 0

  if obj.pDispatch
    ole_addref(obj.pDispatch)
    n = ole_release(obj.pDispatch)
  end

  n
end

.ole_release(dispatch) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/win32ole-pr.rb', line 196

def self.ole_release(dispatch)
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, dispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  release = Win32::API::Function.new(table[2], 'P', 'L')
  release.call(dispatch)
end

.ole_set_safe_array(n, psa, pid, pub, val, dim, vt) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/win32ole-pr.rb', line 590

def self.ole_set_safe_array(n, psa, pid, pub, val, dim, vt)
  var = 0.chr * 16
  i = n
  while i >= 0
    val1 = ole_ary_m_entry(val, pid)
    VariantInit(var)
    p = val2variant_ptr(val1, var, vt)
    if is_all_index_under(pid, pub, dim) == true
      vvt = var[0, 2].unpack('S').first
      dispatch = var[8, 4].unpack('L').first
      if (vvt == VT_DISPATCH || vvt == VT_UNKNOWN) && dispatch == 0
        raise WIN32OLERuntimeError, "element of array does not have IDispatch or IUnknown Interface"
      end
      hr = SafeArrayPutElement(psa, pid.pack('L*'), p)
    end
    if hr != S_OK
      raise RuntimeError, "failed to SafeArrayPutElement"
    end
    pid[i] += 1
    if pid[i] > pub[i]
      pid[i] = 0
      i -= 1
    else
      i = dim - 1
    end
  end
end

.ole_show_help(info, helpcontext = nil) ⇒ Object



1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/win32ole-pr.rb', line 1239

def self.ole_show_help(info, helpcontext=nil)
  if info.is_a?(WIN32OLE_TYPE) || info.is_a?(WIN32OLE_METHOD)
    helpfile = info.helpfile

    if helpfile == ''
      name = info.name
      raise RuntimeError, "no helpfile of '#{name}'"
    end

    helpcontext = info.helpcontext
  else
    helpfile = info
  end

  unless helpfile.is_a?(String)
    raise TypeError, "1st parameter must be (String|WIN32OLE_TYPE|WIN32OLE_METHOD)"
  end

  hwnd = 0

  begin
    htmlHelpA = Win32::API.new('HtmlHelpA', 'LPLL', 'L', 'HHCtrl.ocx')
    hwnd = htmlHelpA.call(GetDesktopWindow(), helpfile, 0xF, helpcontext)
    if hwnd == 0
      hwnd = htmlHelpA.call(GetDesktopWindow(), helpfile, 0, helpcontext)
    end
  rescue Win32::API::LoadLibraryError
  end

  if hwnd == 0
    raise RuntimeError, "failed to open help file `#{helpfile}'"
  end

  nil
end

.ole_type_from_itypeinfo(pTypeInfo) ⇒ Object



2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
# File 'lib/win32ole-pr.rb', line 2142

def self.ole_type_from_itypeinfo(pTypeInfo)
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 88)
  table = table.unpack('L*')
  pTypeInfo = 0.chr * 4
  getContainingTypeLib = Win32::API::Function.new(table[18], 'PPP', 'L')
  getDocumentation = Win32::API::Function.new(table[12], 'PLPPPP', 'L')
  p = 0.chr * 4
  i = 0.chr * 4
  getContainingTypeLib.call(pTypeInfo, p, i)
  return nil if hr != S_OK
  pTypeLib = p.unpack('L').first
  index = i.unpack('L').first
  bstr = 0.chr * 4
  hr = getDocumentation.call(pTypeLib, index, bstr, nil, nil, nil)
  WIN32OLE.ole_release(pTypeLib)
  return nil if hr != S_OK
  str = 0.chr * 256
  wcscpy(str, bstr.unpack('L').first)
  type = WIN32OLE_TYPE.new
  type.oletype_set_member(pTypeInfo, wide_to_multi(str))
  type
end

.ole_typelib_from_itypeinfo(pTypeInfo) ⇒ Object



2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
# File 'lib/win32ole-pr.rb', line 2215

def self.ole_typelib_from_itypeinfo(pTypeInfo)
  lpVtbl = 0.chr * 4
  table = 0.chr * 88
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 88)
  table = table.unpack('L*')
  getContainingTypeLib = Win32::API::Function.new(table[18], 'PPP', 'L')
  p = 0.chr * 4
  index = 0.chr * 4
  getContainingTypeLib.call(pTypeInfo, p, index)
  return nil if hr != S_OK
  pTypeLib = p.unpack('L').first
  retval = WIN32OLE.ole_typelib_from_itypelib(pTypeLib)
  WIN32OLE.ole_release(pTypeLib)
  retval
end

.ole_typelib_from_itypelib(pTypeLib) ⇒ Object



2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
# File 'lib/win32ole-pr.rb', line 2189

def self.ole_typelib_from_itypelib(pTypeLib)
  guid = nil
  lpVtbl = 0.chr * 4
  table = 0.chr * 52
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 52)
  table = table.unpack('L*')
  getLibAttr = Win32::API::Function.new(table[7], 'PP', 'L')
  releaseTLibAttr = Win32::API::Function.new(table[12], 'PP', 'L')
  p = 0.chr * 4
  hr = getLibAttr.call(pTypeLib, p)
  return nil if hr != S_OK
  pTLibAttr = p.unpack('L').first
  tLibAttr = 0.chr * 32
  bstr = 0.chr * 160
  memcpy(tLibAttr, pTLibAttr, 32)
  len = StringFromGUID2(pTLibAttr, bstr, 80)
  if len > 3
    guid = wide_to_multi(bstr)
  end
  major, minor = tLibAttr[24, 4].unpack('SS')
  releaseTLibAttr.call(pTypeLib, pTLibAttr)
  return nil if guid.nil?
  WIN32OLE_TYPELIB.new(guid, major, minor)
end

.ole_types_from_typelib(pTypeLib, classes) ⇒ Object



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File 'lib/win32ole-pr.rb', line 966

def self.ole_types_from_typelib(pTypeLib, classes)
  lpVtbl = 0.chr * 4
  table = 0.chr * 56
  memcpy(lpVtbl, pTypeLib, 4)
  memcpy(table, lpVtbl.unpack('L').first, 40)
  table = table.unpack('L*')
  getTypeInfoCount = Win32::API::Function.new(table[3], 'P', 'L')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLP', 'L')
  getDocumentation = Win32::API::Function.new(table[9], 'PLPPPP', 'L')
  count = getTypeInfoCount.call(pTypeLib)
  for i in 0 ... count
    bstr = 0.chr * 4
    hr = getDocumentation.call(pTypeLib, i, bstr, nil, nil, nil)
    next if hr != S_OK

    p = 0.chr * 4
    hr = getTypeInfo.call(pTypeLib, i, p)
    next if hr != S_OK

    pTypeInfo = p.unpack('L').first
    type = WIN32OLE_TYPE.new
    str = 0.chr * 256
    wcscpy(str, bstr.unpack('L').first)
    type.oletype_set_member(pTypeInfo, wide_to_multi(str))
    classes.push(type)
    ole_release(pTypeInfo)
  end
  classes
end

.ole_uninitializeObject



163
164
165
166
# File 'lib/win32ole-pr.rb', line 163

def self.ole_uninitialize
  OleUninitialize()
  @@ole_initialized = false
end

.ole_val2variant(val, var) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/win32ole-pr.rb', line 659

def self.ole_val2variant(val, var)
  if val.is_a?(WIN32OLE)
    ole_addref(val.pDispatch)
    var[0, 2] = [VT_DISPATCH].pack('S')
    var[8, 4] = [val.pDispatch].pack('L')
    return
  end
  if val.is_a?(WIN32OLE_VARIANT)
    VariantCopy(var, val.var)
    return
  end

  if val.is_a?(Time)
    var[0, 2] = [VT_DATE].pack('S')
    var[8, 8] = [time_object2date(val)].pack('d')
    return
  end

  case val
    when Array
      ole_val_ary2variant_ary(val, var, VT_VARIANT|VT_ARRAY)
    when String
      var[0, 2] = [VT_BSTR].pack('S')
      var[8, 4] = [SysAllocString(multi_to_wide(val, WIN32OLE.codepage))].pack('L')
    when Fixnum
      var[0, 2] = [VT_I4].pack('S')
      var[8, 4] = [val].pack('L')
    when Bignum
      var[0, 2] = [VT_R8].pack('S')
      var[8, 8] = [val].pack('d')
    when Float
      var[0, 2] = [VT_R8].pack('S')
      var[8, 8] = [val].pack('d')
    when TrueClass
      var[0, 2] = [VT_BOOL].pack('S')
      var[8, 2] = [VARIANT_TRUE].pack('S')
    when FalseClass
      var[0, 2] = [VT_BOOL].pack('S')
      var[8, 2] = [VARIANT_FALSE].pack('S')
    when NilClass
      if @@nil_to == VT_ERROR
        var[0, 2] = [VT_ERROR].pack('S')
        var[8, 4] = [DISP_E_PARAMNOTFOUND].pack('L')
      else
        var[0, 2] = [VT_EMPTY].pack('S')
      end
    else
      var[0, 2] = [VT_DISPATCH].pack('S')
      var[8, 4] = [val2dispatch(val)].pack('L')
  end
end

.ole_val2variant2(val, var) ⇒ Object



480
481
482
483
484
# File 'lib/win32ole-pr.rb', line 480

def self.ole_val2variant2(val, var)
  @@nil_to = VT_EMPTY
  ole_val2variant(val, var)
  @@nil_to = VT_ERROR
end

.ole_val2variant_ex(val, var, vt) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/win32ole-pr.rb', line 486

def self.ole_val2variant_ex(val, var, vt)
  if val.nil?
    if vt == VT_VARIANT
      ole_val2variant2(val, var)
    else
      var[0, 2] = [vt & ~VT_BYREF].pack('S')
      if vt & ~VT_BYREF == VT_DISPATCH
        var[8, 4] = [0].pack('L')
      elsif vt & ~VT_BYREF == VT_UNKNOWN
        var[8, 4] = [0].pack('L')
      end
    end
    return
  end
  case vt & ~VT_BYREF
    when VT_I8
      var[0, 2] = [VT_I8].pack('S')
      var[8, 8] = [val].pack('q')
    when VT_UI8
      var[0, 2] = [VT_UI8].pack('S')
      var[8, 8] = [val].pack('Q')
    else
      ole_val2variant2(val, var)
  end
end

.ole_val_ary2variant_ary(val, var, vt) ⇒ Object



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/win32ole-pr.rb', line 618

def self.ole_val_ary2variant_ary(val, var, vt)
  unless val.is_a?(Array)
    raise TypeError, "1st parameter must be Array"
  end

  dim = dimension(val)
  psab = 0.chr * 8 * dim
  pub = Array.new(dim)
  pid = Array.new(dim)

  for i in 0 ... dim
    psab[i*8, 4] = [ary_len_of_dim(val, i)].pack('L')
    psab[i*8+4, 4] = 0.chr * 4
    pub[i] = psab[i*8, 4].unpack('L').first - 1
    pid[i] = 0
  end

  if (vt & ~VT_BYREF) == VT_ARRAY
    vt = (vt | VT_VARIANT)
  end
  psa = SafeArrayCreate(vt & VT_TYPEMASK, dim, psab)
  if psa == 0
    hr = E_OUTOFMEMORY
  else
    hr = SafeArrayLock(psa)
  end
  if hr == S_OK
    ole_set_safe_array(dim-1, psa, pid, pub, val, dim, vt & VT_TYPEMASK)
    hr = SafeArrayUnlock(psa)
  end
  if hr == S_OK
    var[0, 2] = [vt].pack('S')
    var[8, 4] = [psa].pack('L')
  else
    if psa != 0
      SafeArrayDestroy(psa)
    end
  end
  hr
end

.ole_variant2val(pvar) ⇒ Object



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/win32ole-pr.rb', line 711

def self.ole_variant2val(pvar)
  val = nil
  var = 0.chr * 16
  memcpy(var, pvar, 16)
  var = var.unpack('SSSSLL')
  while var[0]== (VT_BYREF | VT_VARIANT)
    pvar = var[4]
    var = 0.chr * 16
    memcpy(var, pvar, 16)
    var = var.unpack('SSSSLL')
  end
  if (var[0] & VT_ARRAY) != 0
    if (var[0] & VT_BYREF) != 0
      ptr = 0.chr * 4
      memcpy(ptr, var[4], 4)
      psa = ptr.unpack('L').first
    else
      psa = var[4]
    end
    return val if psa == 0

    dim = SafeArrayGetDim(psa)
    variant = 0.chr * 16
    VariantInit(variant)
    variant[0, 2] = (var[0] & ~VT_ARRAY) | VT_BYREF
    pid = Array.new(dim)
    plb = Array.new(dim)
    pub = Array.new(dim)

    for i in 0 ... dim
      v = 0.chr * 4
      SafeArrayGetLBound(psa, i+1, v)
      plb[i] = v.unpack('L').first
      v = 0.chr * 4
      SafeArrayGetLBound(psa, i+1, v)
      pid[i] = v.unpack('L').first
      v = 0.chr * 4
      SafeArrayGetUBound(psa, i+1, v)
      pub[i] = v.unpack('L').first
    end
    hr = SafeArrayLock(psa)
    if hr == S_OK
      val = []
      i = 0
      while i < dim
        ary_new_dim(val, pid, plb, dim)
        ref = 0.chr * 4
        hr = SafeArrayPtrOfIndex(psa, pid, ref)
        variant[4, 4] = ref
        if hr == S_OK
          v = ole_variant2val([variant].pack('P').unpack('L').first)
          ary_store_dim(val, pid, plb, dim, v)
        end
        for i in 0 ... dim
          pid[i] += 1
          break if pid[i] <= pub[i]
          pid[i] = plb[i]
        end
      end
      SafeArrayUnlock(psa)
    end
    return val
  end

  case var[0] & ~VT_BYREF
    when VT_EMPTY, VT_NULL
      val = nil
    when VT_I1
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('c').first
      else
        val = [var[4]].pack('L').unpack('c').first
      end
    when VT_UI1
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('C').first
      else
        val = [var[4]].pack('L').unpack('C').first
      end
    when VT_I2
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('s').first
      else
        val = [var[4]].pack('L').unpack('s').first
      end
    when VT_UI2
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('S').first
      else
        val = [var[4]].pack('L').unpack('S').first
      end
    when VT_I4
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('i').first
      else
        val = [var[4]].pack('L').unpack('i').first
      end
    when VT_UI4
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('L').first
      else
        val = var[4]
      end
    when VT_INT
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('i').first
      else
        val = [var[4]].pack('L').unpack('i').first
      end
    when VT_UINT
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('L').first
      else
        val = var[4]
      end
    when VT_I8
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 8
        memcpy(ptr, var[4], 8)
        val = ptr.unpack('q').first
      else
        val = [var[4], var[5]].pack('LL').unpack('q').first
      end
    when VT_UI8
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 8
        memcpy(ptr, var[4], 8)
        val = ptr.unpack('Q').first
      else
        val = [var[4], var[5]].pack('LL').unpack('Q').first
      end
    when VT_R4
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('f').first
      else
        val = [var[4]].pack('L').unpack('f').first
      end
    when VT_R8
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 8
        memcpy(ptr, var[4], 8)
        val = ptr.unpack('d').first
      else
        val = [var[4], var[5]].pack('LL').unpack('d').first
      end
    when VT_BSTR
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        ptr.unpack('L').first
        str = 0.chr * 256
        wcscpy(str, ptr.unpack('L').first)
        val = wide_to_multi(str, WIN32OLE.codepage)
      else
        str = 0.chr * 256
        wcscpy(str, var[4])
        val = wide_to_multi(str, WIN32OLE.codepage)
      end
    when VT_ERROR
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('L').first
      else
        val = var[4]
      end
    when VT_BOOL
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        val = ptr.unpack('S').first != 0
      else
        val = [var[4]].pack('L').unpack('S').first != 0
      end
    when VT_DISPATCH
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        pDispatch = ptr.unpack('L').first
      else
        pDispatch = var[4]
      end
      if pDispatch != 0
        ole_addref(pDispatch)
        val = WIN32OLE.new(pDispatch)
      end
    when VT_UNKNOWN
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 4
        memcpy(ptr, var[4], 4)
        punk = ptr.unpack('L').first
      else
        punk = var[4]
      end
      if punk != 0
        lpVtbl = 0.chr * 4
        table = 0.chr * 28
        memcpy(lpVtbl, punk, 4)
        memcpy(table, lpVtbl.unpack('L').first, 28)
        table = table.unpack('L*')
        p = 0.chr * 4
        queryInterface = Win32::API::Function.new(table[0], 'PPP', 'L')
        hr = queryInterface.call(punk, IID_IDispatch, p)
        if hr == S_OK
          pDispatch = p.unpack('L').first
          val = WIN32OLE.new(pDispatch)
        end
      end
    when VT_DATE
      if (var[0] & VT_BYREF) != 0
        ptr = 0.chr * 8
        memcpy(ptr, var[4], 8)
        date = ptr.unpack('d').first
      else
        date = [var[4], var[5]].pack('LL').unpack('d').first
      end
      d = date.to_i
      t = date - d
      yy, mm, dd = Date.jd_to_civil(2415019+d)
      hh, mi, ss = Date.day_fraction_to_time(t)
      val = "%04d/%02d/%02d %02d:%02d:%02d" % [yy, mm, dd, hh, mi, ss]
    else
      variant = 0.chr * 16
      VariantInit(variant)
      hr = VariantChangeTypeEx(variant, var, @@lcid, 0, VT_BSTR)
      var = variant.unpack('SSSSLL')
      if hr == S_OK && var[0] == VT_BSTR
        str = 0.chr * 256
        wcscpy(str, var[4])
        val = wide_to_multi(str)
      end
      VariantClear(variant)
  end

  val
end

.oleclass_from_typelib(obj, pTypeLib, oleclass) ⇒ Object



996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/win32ole-pr.rb', line 996

def self.oleclass_from_typelib(obj, pTypeLib, oleclass)
  found = false
  lpVtbl = 0.chr * 4
  table = 0.chr * 56
  memcpy(lpVtbl, pTypeLib, 4)
  memcpy(table, lpVtbl.unpack('L').first, 40)
  table = table.unpack('L*')
  getTypeInfoCount = Win32::API::Function.new(table[3], 'P', 'L')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLP', 'L')
  getDocumentation = Win32::API::Function.new(table[9], 'PLPPPP', 'L')
  count = getTypeInfoCount.call(pTypeLib)
  for i in 0 ... count
    break if found
    p = 0.chr * 4
    hr = getTypeInfo.call(pTypeLib, i, p)
    next if hr != S_OK
    pTypeInfo = p.unpack('L').first
    bstr = 0.chr * 4
    hr = getDocumentation.call(pTypeLib, i, bstr, nil, nil, nil)
    next if hr != S_OK
    str = 0.chr * 256
    wcscpy(str, bstr.unpack('L').first)
    typelib = wide_to_multi(str)
    if oleclass == typelib
      obj.oletype_set_member(pTypeInfo, typelib)
      found = true
    end
    ole_release(pTypeInfo)
  end
  found
end

.time_object2date(tm) ⇒ Object



463
464
465
466
467
# File 'lib/win32ole-pr.rb', line 463

def self.time_object2date(tm)
  jd = Date.civil_to_jd(tm.year, tm.month, tm.mday)
  t = Date.time_to_day_fraction(tm.hour, tm.min, tm.sec).to_f
  t + jd - 2415019
end

.val2variant_ptr(val, var, vt) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/win32ole-pr.rb', line 563

def self.val2variant_ptr(val, var, vt)
  ole_val2variant_ex(val, var, vt)
  if (vt & ~VT_BYREF) == VT_VARIANT
    p = var
  else
    if (vt & ~VT_BYREF) != var[0, 2].unpack('S').first
      hr = VariantChangeTypeEx(var, var,
                               @@lcid, 0, vt & ~VT_BYREF)
      if hr != S_OK
        raise RuntimeError, "failed to change type"
      end
    end
    p = get_ptr_of_variant(var)
  end
  if p.nil?
    raise RuntimeError, "failed to get pointer of variant"
  end
  p
end

Instance Method Details

#[](method, *args) ⇒ Object



1819
1820
1821
1822
# File 'lib/win32ole-pr.rb', line 1819

def [](method, *args)
  #ole_invoke(method,args,DISPATCH_PROPERTYGET, true)
  ole_invoke(method, args, DISPATCH_PROPERTYGET, false)
end

#[]=(method, *args) ⇒ Object



1836
1837
1838
1839
# File 'lib/win32ole-pr.rb', line 1836

def []=(method, *args)
  #ole_invoke(method, args, DISPATCH_PROPERTYPUT, true)
  ole_invoke(method, args, DISPATCH_PROPERTYPUT, false)
end

#_getproperty(dispid, args, types) ⇒ Object



1828
1829
1830
# File 'lib/win32ole-pr.rb', line 1828

def _getproperty(dispid, args, types)
  ole_invoke2(dispid, args, types, DISPATCH_PROPERTYGET)
end

#_invoke(dispid, args, types) ⇒ Object



1824
1825
1826
# File 'lib/win32ole-pr.rb', line 1824

def _invoke(dispid, args, types)
  ole_invoke2(dispid, args, types, DISPATCH_METHOD)
end

#_ole_methods(mask) ⇒ Object



2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
# File 'lib/win32ole-pr.rb', line 2065

def _ole_methods(mask)
  methods = []
  p = 0.chr * 4
  hr = typeinfo_from_ole(p)
  pTypeInfo = p.unpack('L').first
  if hr != S_OK
    return methods
  end
  methods.concat(WIN32OLE.ole_methods_from_typeinfo(pTypeInfo, mask))
  WIN32OLE.ole_release(pTypeInfo)
  methods
end

#_setproperty(dispid, args, types) ⇒ Object



1832
1833
1834
# File 'lib/win32ole-pr.rb', line 1832

def _setproperty(dispid, args, types)
  ole_invoke2(dispid, args, types, DISPATCH_PROPERTYPUT)
end

#clsid_from_remote(host, server, clsid) ⇒ Object



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
# File 'lib/win32ole-pr.rb', line 216

def clsid_from_remote(host, server, clsid)
  hr = S_OK
  hlm = 0.chr * 4
  err = RegConnectRegistry(host, HKEY_LOCAL_MACHINE, hlm)
  if err != ERROR_SUCCESS
    return HRESULT_FROM_WIN32(err)
  end
  subkey = "SOFTWARE\\Classes\\" + server + "\\CLSID"
  hpid = 0.chr * 4
  err = RegOpenKeyEx(hlm, subkey, 0, KEY_READ, hpid)
  if err != ERROR_SUCCESS
    return HRESULT_FROM_WIN32(err)
  else
    len = clsid.length.pack('L')
    dwtype = 0.chr * 4
    clsid2 = 0.chr * 100
    err = RegQueryValueEx(hpid, "", nil, dwtype, clsid2, len)
    if err == ERROR_SUCCESS && dwtype.unpack('L').first == REG_SZ
      hr = CLSIDFromString(multi_to_wide(clsid2), clsid)
    else
      hr = HRESULT_FROM_WIN32(err)
    end
    RegCloseKey(hpid)
  end
  RegCloseKey(hlm)
  hr
end

#each(&blk) ⇒ Object

Raises:

  • (ArgumentError)


1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
# File 'lib/win32ole-pr.rb', line 1847

def each(&blk)
  raise ArgumentError, "no block" unless block_given?
  result = 0.chr * 16
  dispParams = 0.chr * 16
  excepinfo = 0.chr * 32
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  argErr = 0.chr * 4
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  invoke = Win32::API::Function.new(table[6], 'PLPLLPPPP', 'L')
  hr = invoke.call(@pDispatch, DISPID_NEWENUM, IID_NULL, @@lcid, DISPATCH_METHOD | DISPATCH_PROPERTYGET, dispParams, result, excepinfo, argErr)
  if hr != S_OK
    VariantClear(result)
    raise WIN32OLERuntimeError, "failed to get IEnum Interface"
  end
  if [VT_UNKNOWN, VT_DISPATCH].include?(result[0, 2].unpack('S').first)
    pUnknown = result[8, 4].unpack('L').first
    lpVtbl = 0.chr * 4
    table = 0.chr * 28
    memcpy(lpVtbl, pUnknown, 4)
    memcpy(table, lpVtbl.unpack('L').first, 28)
    table = table.unpack('L*')
    p = 0.chr * 4
    queryInterface = Win32::API::Function.new(table[0], 'PPP', 'L')
    hr = queryInterface.call(pUnknown, IID_IEnumVARIANT, p)
    pEnum = p.unpack('L').first
  end
  if hr != S_OK || pEnum == 0
    VariantClear(result)
    raise RuntimeError, "failed to get IEnum Interface"
  end

  VariantClear(result)
  begin
    variant = 0.chr * 16
    VariantInit(variant)
    lpVtbl = 0.chr * 4
    table = 0.chr * 16
    memcpy(lpVtbl, pEnum, 4)
    memcpy(table, lpVtbl.unpack('L').first, 16)
    table = table.unpack('L*')
    next_ = Win32::API::Function.new(table[3], 'PLPP', 'L')
    while next_.call(pEnum, 1, variant, nil) == S_OK
      obj = WIN32OLE.ole_variant2val(variant)
      VariantClear(variant)
      VariantInit(variant)
      yield obj
    end
  ensure
    WIN32OLE.ole_release(pEnum)
  end
  nil
end

#invoke(method, *args) ⇒ Object



1815
1816
1817
# File 'lib/win32ole-pr.rb', line 1815

def invoke(method, *args)
  ole_invoke(method, args, DISPATCH_METHOD|DISPATCH_PROPERTYGET, false)
end

#ole_activex_initializeObject



2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
# File 'lib/win32ole-pr.rb', line 2115

def ole_activex_initialize
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  queryInterface = Win32::API::Function.new(table[0], 'PPP', 'L')
  p = 0.chr * 4
  hr = queryInterface.call(pUnknown, IID_IPersistMemory, p)
  pPersistMemory = p.unpack('L').first
  if hr == S_OK
    lpVtbl = 0.chr * 4
    table = 0.chr * 36
    memcpy(lpVtbl, @pDispatch, 4)
    memcpy(table, lpVtbl.unpack('L').first, 36)
    table = table.unpack('L*')
    initNew = Win32::API::Function.new(table[8], 'P', 'L')
    hr = initNew.call(pPersistMemory)
    WIN32OLE.ole_release(pPersistMemory)
    return nil if hr == S_OK
  end
  if hr != S_OK
    raise WIN32OLERuntimeError, "fail to initialize ActiveX control"
  end
  nil
end

#ole_create_dcom(server, host) ⇒ Object



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
# File 'lib/win32ole-pr.rb', line 244

def ole_create_dcom(server, host)
  clsid = 0.chr * 16
  serverw = multi_to_wide(server)
  hostw = multi_to_wide(host)
  hr = CLSIDFromProgID(serverw, clsid)
  if hr != S_OK
    hr = clsid_from_remote(host, server, clsid)
  end
  if hr != S_OK
    hr = CLSIDFromString(serverw, clsid)
  end
  if hr != S_OK
    raise WIN32OLERuntimeError, "unknown OLE server: `#{server}'"
  end
  serverinfo = 0.chr * 16
  serverinfo[4, 4] = [hostw].pack('P')
  multi_qi = 0.chr * 12
  multi_qi[0, 4] = [IID_IDispatch].pack('P')
  hr = CoCreateInstanceEx(clsid, nil, CLSCTX_REMOTE_SERVER, serverinfo, 1, multi_qi)
  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to create DCOM server `#{server}' in `#{host}'"
  end
  ole_set_member(multi_qi[4, 4].unpack('L').first)
  self
end

#ole_excepinfo2msg(execinfo) ⇒ Object



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
# File 'lib/win32ole-pr.rb', line 1392

def ole_excepinfo2msg(execinfo)
  source = nil
  description = nil
  code, _, src, desc, helpfile, helpconext, _, fillin, scode = execinfo.unpack('SSLLLLLLL')
  if fillin != 0
    fnDeferredFillIn = Win32::API::Function.new(fillin, 'P', 'L')
    fnDeferredFillIn.call(execinfo)
  end
  if src != 0
    str = 0.chr * 256
    wcscpy(str, src)
    source = wide_to_multi(str)
  end
  if desc != 0
    str = 0.chr * 256
    wcscpy(str, desc)
    description = wide_to_multi(str)
  end
  if code == 0
    error_msg = "\n    OLE error code:%X in " % scode
  else
    error_msg = "\n    OLE error code:%u in " % code
  end
  if source
    error_msg << source
  else
    error_msg << "<Unknown>"
  end
  error_msg << "\n      "
  if description
    error_msg << description
  else
    error_msg << "<No Description>"
  end
  error_msg
end

#ole_freeObject



1841
1842
1843
1844
1845
# File 'lib/win32ole-pr.rb', line 1841

def ole_free
  WIN32OLE._ole_free(@pDispatch)
  @pDispatch = nil
  nil
end

#ole_func_methodsObject



2090
2091
2092
# File 'lib/win32ole-pr.rb', line 2090

def ole_func_methods
  _ole_methods(INVOKE_FUNC)
end

#ole_get_methodsObject



2082
2083
2084
# File 'lib/win32ole-pr.rb', line 2082

def ole_get_methods
  _ole_methods(INVOKE_PROPERTYGET)
end

#ole_invoke(method, args, flags, is_bracket) ⇒ Object



1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
# File 'lib/win32ole-pr.rb', line 1612

def ole_invoke(method, args, flags, is_bracket)
  excepinfo = 0.chr * 32
  result = 0.chr * 16
  VariantInit(result)
  dp = 0.chr * 16
  argErr = 0.chr * 4
  if !method.is_a?(String) && !method.is_a?(Symbol) && !is_bracket
    raise TypeError, "method is wrong type (expected String or Symbol)"
  end
  if method.is_a?(Symbol)
    method = method.to_s
  end
  if @pDispatch.nil?
    raise RuntimeError, "failed to get dispatch interface"
  end
  if is_bracket
    dispid = DISPID_VALUE
    args.unshift(method)
  else
    wcmdname = multi_to_wide(method)
    lpVtbl = 0.chr * 4
    table = 0.chr * 28
    memcpy(lpVtbl, @pDispatch, 4)
    memcpy(table, lpVtbl.unpack('L').first, 28)
    table = table.unpack('L*')
    getIDsOfNames = Win32::API::Function.new(table[5], 'PPPLLP', 'L')
    p = 0.chr * 4
    hr = getIDsOfNames.call(@pDispatch, IID_NULL, [wcmdname].pack('P'), 1, @@lcid, p)
    dispid = p.unpack('L').first
    if hr != S_OK
      raise WIN32OLERuntimeError, "unknown property or method: `#{method}'"
    end
  end

  param = args[-1]
  dp[12, 4] = [0].pack('L')
  if param.is_a?(Hash)
    cNamedArgs = param.length
    cArgs = cNamedArgs + args.length - 1
    rgvarg = 0.chr * 16 * cArgs
    dp[8, 4] = [cArgs].pack('L')
    pNamedArgs = 0.chr * 4 * (cNamedArgs+1)
    index = 0
    param.each do |key, val|
      if !key.is_a?(String) && !key.is_a?(Symbol)
        for i in 1 .. index
          VariantClear(rgvarg[i*4, 4].unpack('L').first)
        end
        raise TypeError, "wrong argument type (expected String or Symbol)"
      end
      if key.is_a?(Symbol)
        key = key.to_s
      end
      pNamedArgs[(index+1)*4, 4] = [multi_to_wide(key)].pack('P')
      p = rgvarg[index*16, 16]
      VariantInit(p)
      rgvarg[index*16, 16] = p
      p = rgvarg[index*16, 16]
      WIN32OLE.ole_val2variant(val, p)
      rgvarg[index*16, 16] = p
      index += 1
    end
    dp[12, 4] = [index].pack('L')
    pDispID = 0.chr * 4 * (cNamedArgs+1)
    pNamedArgs[0, 4] = [multi_to_wide(method)].pack('P')
    hr = getIDsOfNames.call(@pDispatch, IID_NULL, pNamedArgs, cNamedArgs+1, @@lcid, pDispID)
    if hr != S_OK
      for i in 0 ... cArgs
        p = rgvarg[i*16, 16]
        VariantClear(p)
        rgvarg[i*16, 16] = p
      end
      raise WIN32OLERuntimeError, "failed to get named argument info: `#{method}'"
    end
    dp[4, 4] = [[pDispID].pack('P').unpack('L').first + 4].pack('L')
  else
    cNamedArgs = 0
    cArgs = args.length
    dp[8, 4] = [cArgs].pack('L')
    pNamedArgs = 0.chr * 4 * (cNamedArgs + 1)
    if cArgs > 0
      rgvarg = 0.chr * 16 * cArgs
    end
  end
  if cArgs > cNamedArgs
    realargs = 0.chr * 16 * (cArgs - cNamedArgs + 1)
    for i in cNamedArgs ... cArgs
      n = cArgs - i + cNamedArgs - 1
      p = realargs[n*16, 16]
      VariantInit(p)
      realargs[n*16, 16] = p
      p = rgvarg[n*16, 16]
      VariantInit(p)
      rgvarg[n*16, 16] = p
      param = args[i-cNamedArgs]
      if param.is_a?(WIN32OLE_VARIANT)
        p = rgvarg[n*16, 16]
        VariantCopy(p, param.var)
        rgvarg[n*16, 16] = p
      else
        p = realargs[n*16, 16]
        WIN32OLE.ole_val2variant(param, p)
        realargs[n*16, 16] = p
        rgvarg[n*16, 2] = [VT_VARIANT | VT_BYREF].pack('S')
        rgvarg[n*16+8, 4] = [[realargs].pack('P').unpack('L').first + n*16].pack('L')
        rgvarg[n*16, 16] = p
      end
    end
  end
  dp[0, 4] = [rgvarg].pack('P')
  if (flags & DISPATCH_PROPERTYPUT) != 0
    if cArgs == 0
      raise WIN32OLERuntimeError, "argument error"
    end
    dp[12, 4] = [1].pack('L')
    rgdispidNamedArgs = [DISPID_PROPERTYPUT].pack('L')
    dp[4, 4] = [rgdispidNamedArgs].pack('P')
  end
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  invoke = Win32::API::Function.new(table[6], 'PLPLLPPPP', 'L')
  hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, flags, dp, result, excepinfo, argErr)
  if hr != S_OK
    cArgs = dp[8, 4].unpack('L').first
    if cArgs >= cNamedArgs
      for i in cNamedArgs ... cArgs
        n = cArgs - i + cNamedArgs - 1
        param = args[i-cNamedArgs]
        p = rgvarg[n*16, 16]
        WIN32OLE.ole_val2variant(param, p)
        rgvarg[n*16, 16] = p
      end
      dp[0, 4] = [rgvarg].pack('P')
      excepinfo = 0.chr * 32
      VariantInit(result)
      hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, flags, dp, result, excepinfo, argErr)
      if (hr & DISP_E_EXCEPTION == DISP_E_EXCEPTION || hr & DISP_E_MEMBERNOTFOUND == DISP_E_MEMBERNOTFOUND) && dispid > 0x8000
        excepinfo = 0.chr * 32
        hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, flags, dp, nil, excepinfo, argErr)
      end
      cArgs = dp[8, 4].unpack('L').first
      for i in cNamedArgs ... cArgs
        n = cArgs - i + cNamedArgs - 1
        p = rgvarg[n*16, 16]
        VariantClear(p)
        rgvarg[n*16, 16] = p
      end
    end
    if hr != S_OK
      if cArgs > cNamedArgs
        for i in cNamedArgs ... cArgs
          n = cArgs - i + cNamedArgs - 1
          param = args[i-cNamedArgs]
          p = rgvarg[n*16, 16]
          WIN32OLE.ole_val2variant2(param, p)
          rgvarg[n*16, 16] = p
        end
        dp[0, 4] = [rgvarg].pack('P')
        excepinfo = 0.chr * 32
        VariantInit(result)
        hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, flags, dp, result, excepinfo, argErr)
        cArgs = dp[8, 4].unpack('L').first
        for i in cNamedArgs ... cArgs
          n = cArgs - i + cNamedArgs - 1
          p = rgvarg[n*16, 16]
          VariantClear(p)
          rgvarg[n*16, 16] = p
        end
      end
    end
  end

  if cArgs > cNamedArgs
    for i in cNamedArgs ... cArgs
      n = cArgs - i + cNamedArgs - 1
      param = args[i-cNamedArgs]
      if param.is_a?(WIN32OLE_VARIANT)
        p = realargs[n*16, 16]
        WIN32OLE.ole_val2variant(param, p)
        realargs[n*16, 16] = p
      end
    end
    set_argv(realargs, cNamedArgs, cArgs)
  else
    for i in 0 ... cArgs
      p = rgvarg[i*16, 16]
      VariantClear(p)
      rgvarg[i*16, 16] = p
    end
  end

  if hr != S_OK
    v = ole_excepinfo2msg(excepinfo)
    raise WIN32OLERuntimeError, "(in OLE method `#{method}': #{v})"
  end
  obj = WIN32OLE.ole_variant2val(result)
  VariantClear(result)
  obj
end

#ole_invoke2(dispid, args, types, dispkind) ⇒ Object



1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
# File 'lib/win32ole-pr.rb', line 1480

def ole_invoke2(dispid, args, types, dispkind)
  unless args.is_a?(Array)
    raise TypeError, "wrong argument type #{args.class} (excepted Array)"
  end
  unless types.is_a?(Array)
    raise TypeError, "wrong argument type #{types.class} (excepted Array)"
  end
  result = 0.chr * 16
  excepinfo = 0.chr * 32
  dispParams = 0.chr * 16
  VariantInit(result)
  dispParams[8, 4] = [args.length].pack('L')
  rgvarg = 0.chr * 16 * args.length
  dispParams[0, 4] = [rgvarg].pack('P')
  realargs = 0.chr * 16 * args.length
  j = args.length
  for i in 0 ... args.length
    j-=1
    p = realargs[i*16, 16]
    VariantInit(p)
    realargs[i*16, 16] = p
    p = rgvarg[i*16, 16]
    VariantInit(p)
    rgvarg[i*16, 16] = p
    vt = types[j]
    rgvarg[i*16, 2] = [vt].pack('S')
    param = args[j]
    if param.nil?
      rgvarg[i*16, 2] = realargs[i*16, 2] = [VT_ERROR].pack('S')
      rgvarg[i*16+8, 4] = realargs[i*16+8, 4] = [DISP_E_PARAMNOTFOUND].pack('L')
    else
      if (vt & VT_ARRAY) != 0
        unless param.is_a?(Array)
          raise TypeError, "wrong argument type #{param.class} (excepted Array)"
        end
        rgsabound = [param.length, 0].pack('LL')
        v = vt & ~(VT_ARRAY | VT_BYREF)
        realargs[i*16+8, 4] = [SafeArrayCreate(v, 1, rgsabound)].pack('L')
        realargs[i*16, 2] = [VT_ARRAY | v].pack('S')
        SafeArrayLock(realargs[i*16+8, 4].unpack('L').first)
        arr = 0.chr * 12
        memcpy(arr, realargs[i*16+8, 4].unpack('L').first, 12)
        pb = arr[12, 4].unpack('L').first
        for ent in 0 ... param.length
          velem = 0.chr * 16
          elem = param[ent]
          WIN32OLE.ole_val2variant(elem, velem)
          if v != VT_VARIANT
            VariantChangeTypeEx(velem, velem, @@lcid, 0, v)
          end
          case v
            when VT_VARIANT
              memcpy(pb, velem, 16)
              pb += 16
            when VT_R8, VT_CY, VT_DATE
              memcpy(pb, velem[8, 8], 8)
              pb += 8
            when VT_BOOL, VT_I2, VT_UI2
              memcpy(pb, velem[8, 2], 2)
              pb += 2
            when VT_UI1, VT_I1
              memcpy(pb, vleme[8, 1], 1)
              pb += 1
            else
              memcpy(pb, vleme[8, 4], 4)
              pb += 4
          end
        end
        SafeArrayUnlock(realargs[i*16+8, 4].unpack('L').first)
      else
        p = realargs[i*16, 16]
        WIN32OLE.ole_val2variant(param, p)
        realargs[i*16, 16] = p
        if vt & ~VT_BYREF != VT_VARIANT
          p = realargs[i*16, 16]
          hr = VariantChangeTypeEx(p, p, @@lcid, 0, vt & ~VT_BYREF)
          realargs[i*16, 16] = p
          if hr != S_OK
            raise TypeError, "not valid value"
          end
        end
      end
      if (vt & VT_BYREF) != 0 || vt == VT_VARIANT
        if vt == VT_VARIANT
          rgvarg[i*16, 2] = [VT_VARIANT | VT_BYREF].pack('S')
        end
        case vt & ~VT_BYREF
          when VT_VARIANT
            rgvarg[i*16+8, 4] = [realargs[i*16, 16]].pack('P')
          when VT_R8, VT_CY, VT_DATE
            rgvarg[i*16+8, 4] = [realargs[i*16+8, 8]].pack('P')
          when VT_BOOL, VT_I2, VT_UI2
            rgvarg[i*16+8, 4] = [realargs[i*16+8, 4]].pack('P')
          when VT_UI1, VT_I1
            rgvarg[i*16+8, 4] = [realargs[i*16+8, 1]].pack('P')
          else
            rgvarg[i*16+8, 4] = [realargs[i*16+8, 4]].pack('P')
        end
      else
        rgvarg[i*16+8, 4] = [realargs[i*16+8, 8]].pack('P')
      end
    end
  end
  dispParams[0, 4] = [rgvarg].pack('P')

  if (dispkind & DISPATCH_PROPERTYPUT) != 0
    dispParams[12, 4] = [1].pack('L')
    dispParams[4, 4] = [DISPID_PROPERTYPUT].pack('L')
  end

  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  invoke = Win32::API::Function.new(table[6], 'PLPLLPPPP', 'L')
  argErr = 0.chr * 4
  hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, dispkind, dispParams, result, excepinfo, argErr)
  if hr != S_OK
    v = ole_excepinfo2msg(excepinfo)
    raise WIN32OLERuntimeError, "(in OLE method `<dispatch id:#{dispid}>': #{v})"
  end
  cArgs = dispParams[8, 4].unpack('L').first
  if cArgs > 0
    set_argv(realargs, 0, cArgs)
  end

  obj = WIN32OLE.ole_variant2val(result)
  VariantClear(result)
  obj
end

#ole_method(cmdname) ⇒ Object Also known as: ole_method_help



2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
# File 'lib/win32ole-pr.rb', line 2094

def ole_method(cmdname)
  unless cmdname.is_a?(String)
    raise TypeError, "1st parameter must be String"
  end
  p = 0.chr * 4
  hr = typeinfo_from_ole(p)
  if hr != S_OK
    rasie RuntimeError, "failed to get ITypeInfo"
  end
  pTypeInfo = p.unpack('L').first
  method = WIN32OLE_METHOD.new
  obj = method.olemethod_from_typeinfo(pTypeInfo, cmdname)
  WIN32OLE.ole_release(pTypeInfo)
  if obj.nil?
    raise WIN32OLERuntimeError, "not found #{cmdname}"
  end
  obj
end

#ole_methodsObject



2078
2079
2080
# File 'lib/win32ole-pr.rb', line 2078

def ole_methods
  _ole_methods(INVOKE_FUNC | INVOKE_PROPERTYGET | INVOKE_PROPERTYPUT | INVOKE_PROPERTYPUTREF)
end

#ole_propertyput(property, value) ⇒ Object



1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'lib/win32ole-pr.rb', line 1429

def ole_propertyput(property, value)
  dispIDParam = [DISPID_PROPERTYPUT].pack('L')
  wFlags = DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF
  dispParams = 0.chr * 16
  propertyValue = 0.chr * 32
  argErr = 0.chr * 4

  p = propertyValue[0, 16]
  VariantInit(p)
  propertyValue[0, 16] = p
  p = propertyValue[16, 16]
  VariantInit(p)
  propertyValue[16, 16] = p
  excepinfo = 0.chr * 32

  wproperty = multi_to_wide(property)
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  getIDsOfNames = Win32::API::Function.new(table[5], 'PPPLLP', 'L')
  invoke = Win32::API::Function.new(table[6], 'PLPLLPPPP', 'L')
  p = 0.chr * 4
  hr = getIDsOfNames.call(@pDispatch, IID_NULL, [wproperty].pack('P'), 1, @@lcid, p)
  dispid = p.unpack('L').first
  if hr != S_OK
    raise WIN32OLERuntimeError, "unknown property or method: `#{property}'"
  end

  p = propertyValue[0, 16]
  WIN32OLE.ole_val2variant(value, p)
  propertyValue[0, 16] = p
  dispParams[4, 4] = [dispIDParam].pack('P')
  dispParams[0, 4] = [propertyValue].pack('P')
  dispParams[8, 4] = [1].pack('L')
  dispParams[12, 4] = [1].pack('L')
  hr = invoke.call(@pDispatch, dispid, IID_NULL, @@lcid, wFlags, dispParams, nil, excepinfo, argErr)
  cArgs = dispParams[8, 4].unpack('L').first
  for index in 0 ... cArgs
    p = propertyValue[index*16, 16]
    VariantClear(p)
    propertyValue[index*16, 16] = p
  end
  if hr != S_OK
    v = ole_excepinfo2msg(excepinfo)
    raise WIN32OLERuntimeError, "(in setting property `#{property}': )#{v}"
  end
  nil
end

#ole_put_methodsObject



2086
2087
2088
# File 'lib/win32ole-pr.rb', line 2086

def ole_put_methods
  _ole_methods(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)
end

#ole_query_interface(iid) ⇒ Object



2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
# File 'lib/win32ole-pr.rb', line 2253

def ole_query_interface(iid)
  pBuf = multi_to_wide(iid)
  id = 0.chr * 16
  hr = CLSIDFromString(pBuf, id)
  if hr != S_OK
    raise WIN32OLERuntimeError, "invalid iid: `#{iid}'"
  end
  if @pDispatch.nil?
    raise RuntimeError, "failed to get dispatch interface"
  end
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  queryInterface = Win32::API::Function.new(table[0], 'PPP', 'L')
  p = 0.chr * 4
  hr = queryInterface.call(@pDispatch, id, p)
  if hr != S_OK
    raise WIN32OLERuntimeError, "failed to get interface `#{iid}'"
  end
  pDispatch = p.unpack('L').first
  WIN32OLE.new(pDispatch)
end

#ole_respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
# File 'lib/win32ole-pr.rb', line 2278

def ole_respond_to?(method)
  if !method.is_a?(String) && !method.is_a?(Symbol)
    raise TypeError, "wrong argument type (expected String or Symbol)"
  end
  method = method.to_s if method.is_a?(Symbol)
  wcmdname = multi_to_wide(method)
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  getIDsOfNames = Win32::API::Function.new(table[5], 'PPPLLP', 'L')
  p = 0.chr * 4
  hr = getIDsOfNames.call(@pDispatch, IID_NULL, [wcmdname].pack('P'), 1, @@lcid, p)
  hr == S_OK
end

#ole_set_member(dispatch) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/win32ole-pr.rb', line 179

def ole_set_member(dispatch)
  if @pDispatch
    WIN32OLE.ole_release(@pDispatch)
  end
  @pDispatch = dispatch
  self
end

#ole_typeObject Also known as: ole_obj_help



2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
# File 'lib/win32ole-pr.rb', line 2166

def ole_type
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  pTypeInfo = 0.chr * 4
  getTypeInfo = Win32::API::Function.new(table[4], 'PLLP', 'L')
  hr = getTypeInfo.call(pDispatch, 0, @lcid, pTypeInfo)
  if hr != S_OK
    raise RuntimeError, "failed to GetTypeInfo"
  end
  pTypeInfo = pTypeInfo.unpack('L').first
  type = ole_type_from_itypeinfo(pTypeInfo)
  WIN32OLE.ole_release(pTypeInfo)
  if type.nil?
    riase RuntimeError, "failed to create WIN32OLE_TYPE obj from ITypeInfo"
  end
  type
end

#ole_typelibObject



2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
# File 'lib/win32ole-pr.rb', line 2232

def ole_typelib
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLLP', 'L')
  p = 0.chr * 4
  hr = getTypeInfo.call(pDispatch, 0, @lcid, p)
  if hr != S_OK
    raise RuntimeError, "failed to GetTypeInfo"
  end
  pTypeInfo = p.unpack('L').first
  vtlib = ole_typelib_from_itypeinfo(pTypeInfo)
  WIN32OLE.ole_release(pTypeInfo)
  if vtlib.nil?
    raise RuntimeError, "failed to get type library info."
  end
  vtlib
end

#set_argv(realargs, st, ed) ⇒ Object



1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
# File 'lib/win32ole-pr.rb', line 1380

def set_argv(realargs, st, ed)
  argv = WIN32OLE::ARGV
  argv.clear
  while (ed-=1) > st
    argv.push(WIN32OLE.ole_variant2val(realargs[ed*16, 16]))
    p = realargs[ed*16, 16]
    VariantClear(p)
    realargs[ed*16, 16] = p
  end
  argv
end

#setproperty(property, *args) ⇒ Object



1913
1914
1915
# File 'lib/win32ole-pr.rb', line 1913

def setproperty(property, *args)
  ole_invoke(property, args, DISPATCH_PROPERTYPUT, false)
end

#typeinfo_from_ole(ppti) ⇒ Object



1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
# File 'lib/win32ole-pr.rb', line 1917

def typeinfo_from_ole(ppti)
  lpVtbl = 0.chr * 4
  table = 0.chr * 28
  memcpy(lpVtbl, @pDispatch, 4)
  memcpy(table, lpVtbl.unpack('L').first, 28)
  table = table.unpack('L*')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLLP', 'L')
  p = 0.chr * 4
  hr = getTypeInfo.call(@pDispatch, 0, @@lcid, p)
  if hr != S_OK
    raise rb_eRuntimeError, "failed to GetTypeInfo"
  end
  pTypeInfo = p.unpack('L').first
  lpVtbl = 0.chr * 4
  table = 0.chr * 80
  memcpy(lpVtbl, pTypeInfo, 4)
  memcpy(table, lpVtbl.unpack('L').first, 80)
  table = table.unpack('L*')
  getDocumentation = Win32::API::Function.new(table[12], 'PLPPPP', 'L')
  getContainingTypeLib = Win32::API::Function.new(table[18], 'PPP', 'L')
  bstr = 0.chr * 4
  hr = getDocumentation.call(pTypeInfo, -1, bstr, nil, nil, nil)
  str = 0.chr * 256
  wcscpy(str, bstr.unpack('L').first)
  type = wide_to_multi(str)
  p = 0.chr * 4
  i = 0.chr * 4
  hr = getContainingTypeLib.call(pTypeInfo, p, i)
  WIN32OLE.ole_release(pTypeInfo)
  if hr != S_OK
    raise RuntimeError, "failed to GetContainingTypeLib"
  end
  pTypeLib = p.unpack('L').first
  lpVtbl = 0.chr * 4
  table = 0.chr * 40
  memcpy(lpVtbl, pTypeLib, 4)
  memcpy(table, lpVtbl.unpack('L').first, 40)
  table = table.unpack('L*')
  getTypeInfoCount = Win32::API::Function.new(table[3], 'P', 'L')
  getTypeInfo = Win32::API::Function.new(table[4], 'PLP', 'L')
  getDocumentation = Win32::API::Function.new(table[9], 'PLPPPP', 'L')
  count = getTypeInfoCount.call(pTypeLib)
  for i in 0 ... count
    bstr = 0.chr * 4
    hr = getDocumentation.call(pTypeLib, i, bstr, nil, nil, nil)
    if hr == S_OK
      str = 0.chr * 256
      wcscpy(str, bstr.unpack('L').first)
      if wide_to_multi(str) == type
        p = 0.chr * 4
        hr = getTypeInfo.call(pTypeLib, i, p)
        if hr == S_OK
          ppti[0, 4] = p
          break
        end
      end
    end
  end
  WIN32OLE.ole_release(pTypeLib)
  hr
end

#val2dispatch(val) ⇒ Object



469
470
471
472
473
474
475
476
477
478
# File 'lib/win32ole-pr.rb', line 469

def val2dispatch(val)
  if (pdisp = @@com_hash[val])
    refcount = data[4, 4].unpack('L').first
    pdisp[4, 4] = [refcount+1].pack('L')
  else
    pdisp = [@@com_vtbl].pack('P') + [1, val.object_id].pack('L*')
    @@com_hash[val] = pdisp
  end
  [pdisp].unpack('P').unpack('L').first
end