In Files

  • Win32API/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 Win32API/lib/win32/registry.rb, line 541
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 Win32API/lib/win32/registry.rb, line 495
def self.expand_environ(str)
  str.gsub(/%([^%]+)%/) { ENV[$1] || ENV[$1.upcase] || $& }
end
            
new(hkey, parent, keyname, disposition) click to toggle source

initialize

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

Public Instance Methods

[](name, *rtype) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 690
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 Win32API/lib/win32/registry.rb, line 746
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 Win32API/lib/win32/registry.rb, line 600
def _dump(depth)
  raise TypeError, "can't dump Win32::Registry"
end
            
close() click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 615
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 Win32API/lib/win32/registry.rb, line 611
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 Win32API/lib/win32/registry.rb, line 576
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 Win32API/lib/win32/registry.rb, line 784
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 Win32API/lib/win32/registry.rb, line 779
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 Win32API/lib/win32/registry.rb, line 644
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 Win32API/lib/win32/registry.rb, line 624
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 Win32API/lib/win32/registry.rb, line 809
def flush
  API.FlushKey @hkey
end
            
info() click to toggle source

key information

 
               # File Win32API/lib/win32/registry.rb, line 816
def info
  API.QueryInfoKey(@hkey)
end
            
inspect() click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 593
def inspect
  "\#<Win32::Registry key=#{name.inspect}>"
end
            
keys() click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 658
def keys
  keys_ary = []
  each_key { |key,| keys_ary << key }
  keys_ary
end
            
name() click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 584
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 Win32API/lib/win32/registry.rb, line 607
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 Win32API/lib/win32/registry.rb, line 580
def open?
  !@hkey.nil?
end
            
read(name, *rtype) click to toggle source

reader

 
               # File Win32API/lib/win32/registry.rb, line 667
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 Win32API/lib/win32/registry.rb, line 719
def read_bin(name)
  read(name, REG_BINARY)[1]
end
            
read_i(name) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 715
def read_i(name)
  read(name, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD)[1]
end
            
read_s(name) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 702
def read_s(name)
  read(name, REG_SZ)[1]
end
            
read_s_expand(name) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 706
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 Win32API/lib/win32/registry.rb, line 726
def write(name, type, data)
  case type
  when REG_SZ, REG_EXPAND_SZ
    data = data.to_s + "\00""
  when REG_MULTI_SZ
    data = data.to_a.join("\00"") + "\00\\00""
  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 Win32API/lib/win32/registry.rb, line 772
def write_bin(name, value)
  write name, REG_BINARY, value.to_s
end
            
write_i(name, value) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 768
def write_i(name, value)
  write name, REG_DWORD, value.to_i
end
            
write_s(name, value) click to toggle source
 
               # File Win32API/lib/win32/registry.rb, line 764
def write_s(name, value)
  write name, REG_SZ, value.to_s
end