# File dl/win32/lib/win32/registry.rb, line 542 def self.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) newkey, disp = API.CreateKey(hkey.hkey, subkey, opt, desired) obj = new(newkey, hkey, subkey, disp) if block_given? begin yield obj ensure obj.close end else obj end end
utility functions
# File dl/win32/lib/win32/registry.rb, line 496 def self.expand_environ(str) str.gsub(/%([^%]+)%/) { ENV[$1] || ENV[$1.upcase] || $& } end
initialize
# File dl/win32/lib/win32/registry.rb, line 564 def initialize(hkey, parent, keyname, disposition) @hkey = hkey @parent = parent @keyname = keyname @disposition = disposition @hkeyfinal = [ hkey ] ObjectSpace.define_finalizer self, @@final.call(@hkeyfinal) end
constructors
# File dl/win32/lib/win32/registry.rb, line 527 def self.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) subkey = subkey.chomp('\\') newkey = API.OpenKey(hkey.hkey, subkey, opt, desired) obj = new(newkey, hkey, subkey, REG_OPENED_EXISTING_KEY) if block_given? begin yield obj ensure obj.close end else obj end end
# File dl/win32/lib/win32/registry.rb, line 518 def self.time2wtime(time) time.to_i * 10000000 + 116444736000000000 end
# File dl/win32/lib/win32/registry.rb, line 691 def [](name, *rtype) type, data = read(name, *rtype) case type when REG_SZ, REG_DWORD, REG_QWORD, REG_MULTI_SZ data when REG_EXPAND_SZ Registry.expand_environ(data) else raise TypeError, "Type #{type} is not supported." end end
# File dl/win32/lib/win32/registry.rb, line 747 def []=(name, rtype, value = nil) if value write name, rtype, value else case value = rtype when Integer write name, REG_DWORD, value when String write name, REG_SZ, value when Array write name, REG_MULTI_SZ, value else raise TypeError, "Unexpected type #{value.class}" end end value end
marshalling
# File dl/win32/lib/win32/registry.rb, line 601 def _dump(depth) raise TypeError, "can't dump Win32::Registry" end
# File dl/win32/lib/win32/registry.rb, line 616 def close API.CloseKey(@hkey) @hkey = @parent = @keyname = nil @hkeyfinal[0] = nil end
# File dl/win32/lib/win32/registry.rb, line 612 def create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk) self.class.create(self, subkey, desired, opt, &blk) end
attributes
# File dl/win32/lib/win32/registry.rb, line 577 def created? @disposition == REG_CREATED_NEW_KEY end
# File dl/win32/lib/win32/registry.rb, line 785 def delete_key(name, recursive = false) if recursive open(name, KEY_ALL_ACCESS) do |reg| reg.keys.each do |key| begin reg.delete_key(key, true) rescue Error # end end end API.DeleteKey(@hkey, name) else begin API.EnumKey @hkey, 0 rescue Error return API.DeleteKey(@hkey, name) end raise Error.new(5) ## ERROR_ACCESS_DENIED end end
delete
# File dl/win32/lib/win32/registry.rb, line 780 def delete_value(name) API.DeleteValue(@hkey, name) end
# File dl/win32/lib/win32/registry.rb, line 645 def each_key index = 0 while true begin subkey, wtime = API.EnumKey(@hkey, index) rescue Error break end yield subkey, wtime index += 1 end index end
iterator
# File dl/win32/lib/win32/registry.rb, line 625 def each_value index = 0 while true begin subkey = API.EnumValue(@hkey, index) rescue Error break end begin type, data = read(subkey) rescue Error next end yield subkey, type, data index += 1 end index end
flush
# File dl/win32/lib/win32/registry.rb, line 810 def flush API.FlushKey @hkey end
key information
# File dl/win32/lib/win32/registry.rb, line 817 def info API.QueryInfoKey(@hkey) end
# File dl/win32/lib/win32/registry.rb, line 594 def inspect "\#<Win32::Registry key=#{name.inspect}>" end
# File dl/win32/lib/win32/registry.rb, line 659 def keys keys_ary = [] each_key { |key,| keys_ary << key } keys_ary end
# File dl/win32/lib/win32/registry.rb, line 585 def name parent = self name = @keyname while parent = parent.parent name = parent.keyname + '\\' + name end name end
open/close
# File dl/win32/lib/win32/registry.rb, line 608 def open(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk) self.class.open(self, subkey, desired, opt, &blk) end
# File dl/win32/lib/win32/registry.rb, line 581 def open? !@hkey.nil? end
reader
# File dl/win32/lib/win32/registry.rb, line 668 def read(name, *rtype) type, data = API.QueryValue(@hkey, name) unless rtype.empty? or rtype.include?(type) raise TypeError, "Type mismatch (expect #{rtype.inspect} but #{type} present)" end case type when REG_SZ, REG_EXPAND_SZ [ type, data.chop ] when REG_MULTI_SZ [ type, data.split(/\0/) ] when REG_BINARY [ type, data ] when REG_DWORD [ type, API.unpackdw(data) ] when REG_DWORD_BIG_ENDIAN [ type, data.unpack('N')[0] ] when REG_QWORD [ type, API.unpackqw(data) ] else raise TypeError, "Type #{type} is not supported." end end
# File dl/win32/lib/win32/registry.rb, line 720 def read_bin(name) read(name, REG_BINARY)[1] end
# File dl/win32/lib/win32/registry.rb, line 716 def read_i(name) read(name, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD)[1] end
# File dl/win32/lib/win32/registry.rb, line 703 def read_s(name) read(name, REG_SZ)[1] end
# File dl/win32/lib/win32/registry.rb, line 707 def read_s_expand(name) type, data = read(name, REG_SZ, REG_EXPAND_SZ) if type == REG_EXPAND_SZ Registry.expand_environ(data) else data end end
writer
# File dl/win32/lib/win32/registry.rb, line 727 def write(name, type, data) case type when REG_SZ, REG_EXPAND_SZ data = data.to_s + "\0" when REG_MULTI_SZ data = data.to_a.join("\0") + "\0\0" when REG_BINARY data = data.to_s when REG_DWORD data = API.packdw(data.to_i) when REG_DWORD_BIG_ENDIAN data = [data.to_i].pack('N') when REG_QWORD data = API.packqw(data.to_i) else raise TypeError, "Unsupported type #{type}" end API.SetValue(@hkey, name, type, data, data.length) end
# File dl/win32/lib/win32/registry.rb, line 773 def write_bin(name, value) write name, REG_BINARY, value.to_s end