Module: WIN32OLE::Utils

Instance Method Summary collapse

Instance Method Details

#all_methods(typeinfo, &block) ⇒ Object

MRI: olemethod_from_typeinfo



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/win32ole/utils.rb', line 33

def all_methods(typeinfo, &block) # MRI: olemethod_from_typeinfo
  return unless typeinfo # Not all ole servers have this info

  # Find method in this type.
  ret = find_all_methods_in(nil, typeinfo, &block)
  return ret if ret

  # Now check all other type impls
  typeinfo.impl_types_count.times do |i|
    begin
      href = typeinfo.get_ref_type_of_impl_type(i)
      ref_typeinfo = typeinfo.get_ref_type_info(href)
      ret = find_all_methods_in(typeinfo, ref_typeinfo, &block)
      return ret if ret
    rescue ComFailException => e
      puts "Error getting impl types #{e}"
    end
  end
  nil
end

#all_vars(typeinfo) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/win32ole/utils.rb', line 105

def all_vars(typeinfo)
  typeinfo.vars_count.times do |i|
    desc = typeinfo.get_var_desc(i)
    next unless desc
    names = typeinfo.get_names(desc.memid)
    next if !names || names.length == 0
    name = names[0]
    next unless name
    yield desc, name
  end      
end

#find_all_methods_in(old_typeinfo, typeinfo, &block) ⇒ Object

MRI: ole_method_sub



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/win32ole/utils.rb', line 55

def find_all_methods_in(old_typeinfo, typeinfo, &block)
  typeinfo.funcs_count.times do |i|
    begin
      desc = typeinfo.get_func_desc(i)
      docs = typeinfo.get_documentation(desc.memid)
      ret = yield typeinfo, old_typeinfo, desc, docs, i
      return ret if ret
    rescue ComFailException => e
      puts "Error getting method info #{e}"
    end
  end
  nil
end

#find_all_typeinfo(typelib) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/win32ole/utils.rb', line 95

def find_all_typeinfo(typelib)
  typelib.type_info_count.times do |i|
    docs = typelib.get_documentation(i)
    next unless docs
    info = typelib.get_type_info(i)
    next unless info
    yield info, docs
  end      
end

#load_typelib(path_reg, arch) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/win32ole/utils.rb', line 84

def load_typelib(path_reg, arch)
  path = path_reg.open(arch) { |r| r.read(nil) }[1]
#      puts "PATH = #{path}"
  begin
    org.racob.com.Automation.loadTypeLib(path)
  rescue ComFailException => e
#        puts "Failed to load #{name} fom #{path} because: #{e}"
    nil
  end
end

#methods_with_flag(flag) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/win32ole/utils.rb', line 22

def methods_with_flag(flag)
  members = []
  all_methods(typeinfo_from_ole) do |typeinfo, old_typeinfo, desc, docs, i|
    if desc.invkind & flag
      members << WIN32OLE_METHOD.new(nil, typeinfo, old_typeinfo, desc, docs, i)
    end
    nil
  end
  members
end

#reg_each_key_for(reg, subkey, &block) ⇒ Object



117
118
119
120
121
# File 'lib/win32ole/utils.rb', line 117

def reg_each_key_for(reg, subkey, &block)
  reg.open(subkey) do |subkey_reg|
    subkey_reg.each_key { |key, wtime| block.call(subkey_reg, key) }
  end
end

#registry_subkey(reg, *valid_subkeys) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/win32ole/utils.rb', line 145

def registry_subkey(reg, *valid_subkeys)
  reg.each_key do |inner, wtime|
    reg_each_key_for(reg, inner) do |subkey_reg, subkey|
      yield subkey_reg, subkey if valid_subkeys.include? subkey
    end
  end
end

#SafeStringValue(str) ⇒ Object

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
# File 'lib/win32ole/utils.rb', line 8

def SafeStringValue(str)
  return str if str.kind_of?(::String)
  if str.respond_to?(:to_str)
    str = str.to_str
    return str if str.kind_of?(::String)
  end
  raise TypeError
end

#search_registry(typelib_name) ⇒ Object

MRI: oletypelib_search_registry



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/win32ole/utils.rb', line 153

def search_registry(typelib_name) # MRI: oletypelib_search_registry
  typelib_registry_each_guid_version do |guid, version, reg|
    name = reg.read(nil)[1] || ''
    registry_subkey(reg, 'win32', 'win64') do |arch_reg, arch|
      type_lib = load_typelib(arch_reg, arch)
#          puts "GUID #{guid} #{version} #{arch} #{type_lib}"
      return type_lib if type_lib && name == typelib_name
    end
  end
  nil
end

#typedesc_value(vt, type_details = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/win32ole/utils.rb', line 165

def typedesc_value(vt, type_details=nil)
  type_string = WIN32OLE::VARIANT.variant_to_string(vt) || "Unknown Type #{vt}"

  type_details << type_string if type_details

  if vt == WIN32OLE::VARIANT::VT_PTR && type_details
    # TODO: Add detail logic
  end

  type_details ? type_details : type_string
end

#typeinfo_from_oleObject

MRI: typeinfo_from_ole



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/win32ole/utils.rb', line 69

def typeinfo_from_ole # MRI: typeinfo_from_ole
  typeinfo = type_info
  docs = typeinfo.get_documentation(-1)
  type_lib = typeinfo.get_containing_type_lib
  type_lib.get_type_info_count.times do |i|
    begin
      tdocs = type_lib.get_documentation(i)
      return type_lib.get_type_info(i) if tdocs.name == docs.name
    rescue ComFailException => e
      # We continue on failure. 
    end
  end
  type_info # Actually MRI seems like it could fail in weird case
end

#typelib_registry_each_guid_versionObject

Walks all guid/clsid entries and yields every single version of those entries to the supplied block. See search_registry as an example of its usage.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/win32ole/utils.rb', line 126

def typelib_registry_each_guid_version
  Win32::Registry::HKEY_CLASSES_ROOT.open('TypeLib') do |reg| 
    reg.each_key do |guid, wtime|
      reg.open(guid) do |guid_reg|
        guid_reg.each_key do |version_string, wtime|
          version = version_string.to_f
          begin
            guid_reg.open(version_string) do |version_reg|
              yield guid, version, version_reg
            end
          rescue Win32::Registry::Error => e
            # Version Entry may not contain anything. Skip.
          end
        end
      end
    end
  end
end

#WIN32OLE_TYPEValue(value) ⇒ Object

Raises:

  • (TypeError)


17
18
19
20
# File 'lib/win32ole/utils.rb', line 17

def WIN32OLE_TYPEValue(value)
  raise TypeError.new("1st argument should be WIN32OLE_TYPE object") unless value.kind_of? WIN32OLE_TYPE
  value
end