Class: WIN32OLE_TYPELIB

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

Direct Known Subclasses

P2::Library

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ WIN32OLE_TYPELIB

Returns a new instance of WIN32OLE_TYPELIB.



2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
# File 'lib/win32ole-pr.rb', line 2641

def initialize(*args)
  WIN32OLE.ole_initialize()
  @pTypeInfo = nil
  typelib = args[0]
  if args.length>0
    if !typelib.is_a?(String)
      raise TypeError, "typelib is wrong type (expected String)"
    end

    found = WIN32OLE_TYPELIB.oletypelib_search_registry(self, typelib)
    if !found
      found = WIN32OLE_TYPELIB.oletypelib_search_registry2(self, args)
    end
    if !found
      buf = multi_to_wide(typelib)
      p = 0.chr * 4
      hr = LoadTypeLibEx(buf, REGKIND_NONE, p)
      if hr == S_OK
        pTypeLib = p.unpack('L').first
        retval = WIN32OLE.ole_typelib_from_itypelib(pTypeLib)
        WIN32OLE.ole_release(pTypeLib)
        if retval
          found = true
          oletypelib_set_member(retval.name, retval.guid, retval.version)
        end
      end
    end

    if !found
      raise WIN32OLERuntimeError, "not found type library `#{typelib}`"
    end
  end
  self
end

Class Method Details

.make_version_str(major, minor) ⇒ Object



2507
2508
2509
2510
2511
2512
2513
2514
# File 'lib/win32ole-pr.rb', line 2507

def self.make_version_str(major, minor)
  return nil if major.nil?
  version = major.dup
  if minor
    version << '.' + minor
  end
  version
end

.oletypelib_search_registry(obj, typelib) ⇒ Object



2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
# File 'lib/win32ole-pr.rb', line 2577

def self.oletypelib_search_registry(obj, typelib)
  found = false
  htypelib = 0.chr * 4
  err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", htypelib)
  if err != ERROR_SUCCESS
    return false
  end
  i = 0
  htypelib = htypelib.unpack('L').first
  while !found
    guid = reg_enum_key(htypelib, i)
    break if guid.nil?
    hguid = 0.chr * 4
    err = reg_open_vkey(htypelib, guid, hguid)
    if (err != ERROR_SUCCESS)
      i += 1
      next
    end
    j = 0
    hguid = hguid.unpack('L').first
    while !found
      ver = reg_enum_key(hguid, j)
      break if ver.nil?
      hversion = 0.chr * 4
      err = reg_open_vkey(hguid, ver, hversion)
      if err != ERROR_SUCCESS
        j += 1
        next
      end
      hversion = hversion.unpack('L').first
      tlib = reg_get_val(hversion, nil)
      if tlib.nil?
        RegCloseKey(hversion)
        j += 1
        next
      end
      if typelib == tlib
        obj.oletypelib_set_member(typelib, guid, ver)
        found = true
      end
      RegCloseKey(hversion)
      j += 1
    end
    RegCloseKey(hguid)
    i += 1
  end
  RegCloseKey(htypelib)
  found
end

.oletypelib_search_registry2(obj, args) ⇒ Object



2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
# File 'lib/win32ole-pr.rb', line 2516

def self.oletypelib_search_registry2(obj, args)
  found = false
  guid = args[0]

  version = make_version_str(args[1], args[2])

  htypelib = 0.chr * 4
  err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", htypelib)
  return false if err != ERROR_SUCCESS
  hguid = 0.chr * 4
  err = reg_open_vkey(htypelib, guid, hguid)
  if err != ERROR_SUCCESS
    RegCloseKey(htypelib)
    return false
  end
  if version
    hversion = 0.chr * 4
    err = reg_open_vkey(hguid, version_str, hversion)
    if err == ERROR_SUCCESS
      tlib = reg_get_val(hversion, nil)
      if tlib
        typelib = tlib
      end
    end
    RegCloseKey(hversion)
  else
    fver = 0.0;
    j = 0
    while true
      ver = reg_enum_key(hguid, j)
      break if ver.nil?
      hversion = 0.chr * 4
      err = reg_open_vkey(hguid, ver, hversion)
      if err != ERROR_SUCCESS
        j += 1
        next
      end
      tlib = reg_get_val(hversion, nil)
      if tlib.nil?
        RegCloseKey(hversion)
        j += 1
        next
      end
      if fver < ver.to_f
        fver = ver.to_f
        version = ver.dup
        typelib = tlib.dup
      end
      RegCloseKey(hversion)
      j += 1
    end
  end
  RegCloseKey(hguid)
  RegCloseKey(htypelib)
  if typelib
    found = true
    obj.oletypelib_set_member(typelib, guid, version)
  end
  found
end

.reg_enum_key(hkey, i) ⇒ Object



2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/win32ole-pr.rb', line 2335

def self.reg_enum_key(hkey, i)
  buf = 0.chr * 256
  size_buf = [256].pack('L')
  err = RegEnumKeyEx(hkey, i, buf, size_buf, nil, nil, nil, nil)
  if (err == ERROR_SUCCESS)
    return buf.strip
  end
  nil
end

.reg_get_typelib_file_path(hkey) ⇒ Object



2627
2628
2629
2630
2631
2632
2633
# File 'lib/win32ole-pr.rb', line 2627

def self.reg_get_typelib_file_path(hkey)
  path = reg_get_val2(hkey, "win32")
  if path.nil?
    path = reg_get_val2(hkey, "win16")
  end
  path
end

.reg_get_val(hkey, subkey) ⇒ Object



2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
# File 'lib/win32ole-pr.rb', line 2345

def self.reg_get_val(hkey, subkey)
  dwtype = 0.chr * 4
  size = 0.chr * 4
  val = nil
  err = RegQueryValueEx(hkey, subkey, nil, dwtype, nil, size)
  if (err == ERROR_SUCCESS)
    pbuf = 0.chr * (size.unpack('L').first + 1)
    err = RegQueryValueEx(hkey, subkey, nil, dwtype, pbuf, size)
    if (err == ERROR_SUCCESS)
      val = pbuf.strip
    end
  end
  val
end

.reg_get_val2(hkey, subkey) ⇒ Object



2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
# File 'lib/win32ole-pr.rb', line 2360

def self.reg_get_val2(hkey, subkey)
  val = nil
  hsubkey = 0.chr * 4
  err = RegOpenKeyEx(hkey, subkey, 0, KEY_READ, hsubkey)
  if (err == ERROR_SUCCESS)
    hsubkey = hsubkey.unpack('L').first
    val = reg_get_val(hsubkey, nil)
    RegCloseKey(hsubkey)
  end
  if val.nil?
    val = reg_get_val(hkey, subkey)
  end
  val
end

.reg_open_key(hkey, name, phkey) ⇒ Object



2327
2328
2329
# File 'lib/win32ole-pr.rb', line 2327

def self.reg_open_key(hkey, name, phkey)
  RegOpenKeyEx(hkey, name, 0, KEY_READ, phkey)
end

.reg_open_vkey(hkey, key, phkey) ⇒ Object



2331
2332
2333
# File 'lib/win32ole-pr.rb', line 2331

def self.reg_open_vkey(hkey, key, phkey)
  reg_open_key(hkey, key, phkey)
end

.typelib_file(ole) ⇒ Object



2501
2502
2503
2504
2505
# File 'lib/win32ole-pr.rb', line 2501

def self.typelib_file(ole)
  file = typelib_file_from_clsid(ole)
  return file if file
  typelib_file_from_typelib(ole)
end

.typelib_file_from_clsid(ole) ⇒ Object



2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
# File 'lib/win32ole-pr.rb', line 2413

def self.typelib_file_from_clsid(ole)
  path = 0.chr * 256
  hroot = 0.chr * 4
  err = reg_open_key(HKEY_CLASSES_ROOT, "CLSID", hroot)
  if err != ERROR_SUCCESS
    return nil
  end
  hclsid = 0.chr * 4
  hroot = hroot.unpack('L').first
  err = reg_open_key(hroot, ole, hclsid)
  if err != ERROR_SUCCESS
    RegCloseKey(hroot)
    return nil
  end
  typelib = reg_get_val2(hclsid, "InprocServer32")
  RegCloseKey(hroot)
  RegCloseKey(hclsid)
  if typelib
    ExpandEnvironmentStrings(typelib, path, 256)
    typelib = path.strip
  end
  typelib
end

.typelib_file_from_typelib(ole) ⇒ Object



2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'lib/win32ole-pr.rb', line 2437

def self.typelib_file_from_typelib(ole)
  found = false
  htypelib = 0.chr * 4
  err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", htypelib)
  if err != ERROR_SUCCESS
    return nil
  end
  htypelib = htypelib.unpack('L').first
  i = 0
  while !found
    clsid = reg_enum_key(htypelib, i)
    break if clsid.nil?
    hclsid = 0.chr * 4
    err = reg_open_vkey(htypelib, clsid, hclsid)
    if err != ERROR_SUCCESS
      i += 1
      next
    end
    hclsid = hclsid.unpack('L').first
    fver = 0
    j = 0
    while !found
      ver = reg_enum_key(hclsid, j)
      break if ver.nil?
      hversion = 0.chr * 4
      err = reg_open_vkey(hclsid, ver, hversion)
      if err != ERROR_SUCCESS || fver > ver.to_f
        j += 1
        next
      end
      hversion = hversion.unpack('L').first
      fver = ver.to_f
      typelib = reg_get_val(hversion, nil)
      if typelib.nil?
        j += 1
        next
      end
      if typelib == ole
        k = 0
        while !found
          lang = reg_enum_key(hversion, k)
          break if lang.nil?
          hlang = 0.chr * 4
          err = reg_open_vkey(hversion, lang, hlang)
          if err == ERROR_SUCCESS
            hlang = hlang.unpack('L').first
            if (file = reg_get_typelib_file_path(hlang))
              found = true
            end
            RegCloseKey(hlang)
          end
          k += 1
        end
      end
      RegCloseKey(hversion)
      j += 1
    end
    RegCloseKey(hclsid)
    i += 1
  end
  RegCloseKey(htypelib)
  file
end

.typelibsObject



2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
# File 'lib/win32ole-pr.rb', line 2375

def self.typelibs
  typelibs = []
  typelib = nil
  htypelib = [0].pack('L')
  err = reg_open_key(HKEY_CLASSES_ROOT, "TypeLib", htypelib)
  return typelibs if err != ERROR_SUCCESS
  i = 0
  htypelib = htypelib.unpack('L').first
  while true
    guid = reg_enum_key(htypelib, i)
    break if guid.nil?
    hguid = 0.chr * 4
    err = reg_open_vkey(htypelib, guid, hguid)
    if err != ERROR_SUCCESS
      i += 1
      next
    end
    j = 0
    hguid = hguid.unpack('L').first
    while true
      version = reg_enum_key(hguid, j)
      break if version.nil?

      if (name = reg_get_val2(hguid, version))
        typelib = WIN32OLE_TYPELIB.new
        typelib.oletypelib_set_member(name, guid, version)
        typelibs.push(typelib)
      end

      j += 1
    end
    RegCloseKey(hguid)
    i += 1
  end
  RegCloseKey(htypelib)
  typelibs
end

Instance Method Details

#guidObject



2676
2677
2678
# File 'lib/win32ole-pr.rb', line 2676

def guid
  @guid
end

#inspectObject



2805
2806
2807
# File 'lib/win32ole-pr.rb', line 2805

def inspect
  "#<#{self.class}:#{self.to_s}>"
end

#library_nameObject



2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
# File 'lib/win32ole-pr.rb', line 2781

def library_name
  p = 0.chr * 4
  oletypelib2itypelib(p)
  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')
  bstr = 0.chr * 4
  getDocumentation.call(pTypeLib, -1, bstr, nil, nil, nil)
  if hr != S_OK
    WIN32OLE.ole_release(pTypeLib)
    raise WIN32OLERuntimeError, "failed to get library name"
  end
  WIN32OLE.ole_release(pTypeLib)
  str = 0.chr * 256
  wcscpy(str, bstr.unpack('L').first)
  wide_to_multi(str)
end

#major_versionObject



2690
2691
2692
# File 'lib/win32ole-pr.rb', line 2690

def major_version
  @version.split('.')[0]
end

#minor_versionObject



2694
2695
2696
# File 'lib/win32ole-pr.rb', line 2694

def minor_version
  @version.split('.')[1]
end

#nameObject Also known as: to_s



2680
2681
2682
# File 'lib/win32ole-pr.rb', line 2680

def name
  @name
end

#ole_typesObject Also known as: ole_classes



2739
2740
2741
2742
2743
2744
2745
2746
2747
# File 'lib/win32ole-pr.rb', line 2739

def ole_types()
  classes = []
  p = 0.chr * 4
  oletypelib2itypelib(p)
  pTypeLib = p.unpack('L').first
  WIN32OLE.ole_types_from_typelib(pTypeLib, classes)
  WIN32OLE.ole_release(pTypeLib)
  classes
end

#oletypelib2itypelib(pptl) ⇒ Object



2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
# File 'lib/win32ole-pr.rb', line 2726

def oletypelib2itypelib(pptl)
  path = self.path
  if path
    pbuf = multi_to_wide(path)
    hr = LoadTypeLibEx(pbuf, REGKIND_NONE, pptl)
    if hr != S_OK
      raise WIN32OLERuntimeError, "failed to LoadTypeLibEx from `#{path}'"
    end
  else
    raise WIN32OLERuntimeError, "failed to get type library path"
  end
end

#oletypelib_path(guid, version) ⇒ Object



2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
# File 'lib/win32ole-pr.rb', line 2698

def oletypelib_path(guid, version)
  path = nil
  key = "TypeLib\\" + guid+ "\\" + version
  hkey = 0.chr * 4
  err = WIN32OLE_TYPELIB.reg_open_vkey(HKEY_CLASSES_ROOT, key, hkey)
  return nil if err != ERROR_SUCCESS
  hkey = hkey.unpack('L').first
  k = 0
  while path.nil?
    lang = WIN32OLE_TYPELIB.reg_enum_key(hkey, k)
    break if lang.nil?
    hlang = 0.chr * 4
    err = WIN32OLE_TYPELIB.reg_open_vkey(hkey, lang, hlang)
    hlang = hlang.unpack('L').first
    if err == ERROR_SUCCESS
      path = WIN32OLE_TYPELIB.reg_get_typelib_file_path(hlang)
      RegCloseKey(hlang)
    end
    k += 1
  end
  RegCloseKey(hkey)
  path
end

#oletypelib_set_member(name, guid, version) ⇒ Object



2635
2636
2637
2638
2639
# File 'lib/win32ole-pr.rb', line 2635

def oletypelib_set_member(name, guid, version)
  @name = name
  @guid = guid
  @version = version
end

#pathObject



2722
2723
2724
# File 'lib/win32ole-pr.rb', line 2722

def path
  oletypelib_path(@guid, @version)
end

#versionObject



2686
2687
2688
# File 'lib/win32ole-pr.rb', line 2686

def version
  @version
end

#visible?Boolean

Returns:

  • (Boolean)


2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
# File 'lib/win32ole-pr.rb', line 2751

def visible?
  visible = true
  p = 0.chr * 4
  oletypelib2itypelib(p)
  pTypeLib = p.unpack('L').first
  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
  getLibAttr.call(pTypeLib, p)
  if hr != S_OK
    WIN32OLE.ole_release(pTypeLib)
    raise WIN32OLERuntimeError, "failed to get TLIBATTR information"
  end
  pTLibAttr = p.unpack('L').first
  tLibAttr = 0.chr * 32
  memcpy(tLibAttr, pTLibAttr, 32)
  wLibFlags = tLibAttr[28, 2].unpack('S').first
  if wLibFlags == 0 || (wLibFlags & LIBFLAG_FRESTRICTED)!=0 || (wLibFlags & LIBFLAG_FHIDDEN)!=0
    visible = false
  end
  releaseTLibAttr.call(pTypeLib, pTLibAttr)
  WIN32OLE.ole_release(pTypeLib)
  visible
end