In Files

  • dl/win32/lib/win32/registry.rb

Win32::Registry

Attributes

disposition[R]
hkey[R]
keyname[R]
parent[R]

Public Class Methods

create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) click to toggle source
 
               # 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
            
expand_environ(str) click to toggle source

utility functions

 
               # File dl/win32/lib/win32/registry.rb, line 496
def self.expand_environ(str)
  str.gsub(/%([^%]+)%/) { ENV[$1] || ENV[$1.upcase] || $& }
end
            
new(hkey, parent, keyname, disposition) click to toggle source

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
            
open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) click to toggle source

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
            
time2wtime(time) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 518
def self.time2wtime(time)
  time.to_i * 10000000 + 116444736000000000
end
            
type2name(type) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 510
def self.type2name(type)
  @@type2name[type] || type.to_s
end
            
wtime2time(wtime) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 514
def self.wtime2time(wtime)
  Time.at((wtime - 116444736000000000) / 10000000)
end
            

Public Instance Methods

[](name, *rtype) click to toggle source
 
               # 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
            
[]=(name, rtype, value = nil) click to toggle source
 
               # 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
            
_dump(depth) click to toggle source

marshalling

 
               # File dl/win32/lib/win32/registry.rb, line 601
def _dump(depth)
  raise TypeError, "can't dump Win32::Registry"
end
            
close() click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 616
def close
  API.CloseKey(@hkey)
  @hkey = @parent = @keyname = nil
  @hkeyfinal[0] = nil
end
            
create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk) click to toggle source
 
               # 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
            
created?() click to toggle source

attributes

 
               # File dl/win32/lib/win32/registry.rb, line 577
def created?
  @disposition == REG_CREATED_NEW_KEY
end
            
delete(name) click to toggle source
Alias for: delete_value
delete_key(name, recursive = false) click to toggle source
 
               # 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_value(name) click to toggle source

delete

 
               # File dl/win32/lib/win32/registry.rb, line 780
def delete_value(name)
  API.DeleteValue(@hkey, name)
end
            
Also aliased as: delete
each() click to toggle source
Alias for: each_value
each_key() click to toggle source
 
               # 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
            
each_value() click to toggle source

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
            
Also aliased as: each
flush() click to toggle source

flush

 
               # File dl/win32/lib/win32/registry.rb, line 810
def flush
  API.FlushKey @hkey
end
            
info() click to toggle source

key information

 
               # File dl/win32/lib/win32/registry.rb, line 817
def info
  API.QueryInfoKey(@hkey)
end
            
inspect() click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 594
def inspect
  "\#<Win32::Registry key=#{name.inspect}>"
end
            
keys() click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 659
def keys
  keys_ary = []
  each_key { |key,| keys_ary << key }
  keys_ary
end
            
name() click to toggle source
 
               # 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(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk) click to toggle source

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
            
open?() click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 581
def open?
  !@hkey.nil?
end
            
read(name, *rtype) click to toggle source

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
            
read_bin(name) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 720
def read_bin(name)
  read(name, REG_BINARY)[1]
end
            
read_i(name) click to toggle source
 
               # 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
            
read_s(name) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 703
def read_s(name)
  read(name, REG_SZ)[1]
end
            
read_s_expand(name) click to toggle source
 
               # 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
            
write(name, type, data) click to toggle source

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
            
write_bin(name, value) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 773
def write_bin(name, value)
  write name, REG_BINARY, value.to_s
end
            
write_i(name, value) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 769
def write_i(name, value)
  write name, REG_DWORD, value.to_i
end
            
write_s(name, value) click to toggle source
 
               # File dl/win32/lib/win32/registry.rb, line 765
def write_s(name, value)
  write name, REG_SZ, value.to_s
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus