类 Win32::Registry

属性

disposition[R]

Disposition 值 (REG_CREATED_NEW_KEY 或 REG_OPENED_EXISTING_KEY)。

hkey[R]

返回键句柄值。

keyname[R]

Registry.openRegistry.create 方法的子键值相同。

parent[R]

Win32::Registry 父键对象,如果为预定义键则为 nil。

公共类方法

create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) { |obj| ... } 点击切换源代码

Registry.create(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED)

Registry.create(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) { |reg| … }

在 key 下创建或打开注册表键 subkey。您可以使用预定义键 HKEY_*(参见 Constants)。

如果 subkey 已经存在,则打开 key,并且 Registry#created? 方法将返回 false。

如果提供了代码块,则 key 将自动关闭。

# File win32/lib/win32/registry.rb, line 457
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) 点击切换源代码

将 %w+% 替换为 % 符号之间内容的环境值。此方法用于 REG_EXPAND_SZ。

有关详细信息,请参阅 expandEnvironmentStrings Win32 API。

# File win32/lib/win32/registry.rb, line 373
def self.expand_environ(str)
  str.gsub(Regexp.compile("%([^%]+)%".encode(str.encoding))) {
    v = $1.encode(LOCALE)
    (e = ENV[v] || ENV[v.upcase]; e.encode(str.encoding) if e) ||
    $&
  }
end
new(hkey, parent, keyname, disposition) 点击切换源代码

初始化

# File win32/lib/win32/registry.rb, line 479
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) { |obj| ... } 点击切换源代码

Registry.open(key, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED)

Registry.open(key, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) { |reg| … }

打开键 key 下的注册表键 subkey。key 是父键的 Win32::Registry 对象。您可以使用预定义的键 HKEY_*(参见 Constants)desired 和 opt 是访问掩码和键选项。有关详细信息,请参阅 MSDN。如果提供了代码块,则键将自动关闭。

# File win32/lib/win32/registry.rb, line 429
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) 点击切换源代码

将 Time 对象或 Integer 对象转换为 64 位 FILETIME。

# File win32/lib/win32/registry.rb, line 409
def self.time2wtime(time)
  time.to_i * 10000000 + 116444736000000000
end
type2name(type) 点击切换源代码

将注册表类型值转换为可读字符串。

# File win32/lib/win32/registry.rb, line 395
def self.type2name(type)
  @@type2name[type] || type.to_s
end
wtime2time(wtime) 点击切换源代码

将 64 位 FILETIME 整数转换为 Time 对象。

# File win32/lib/win32/registry.rb, line 402
def self.wtime2time(wtime)
  Time.at((wtime - 116444736000000000) / 10000000)
end

公共实例方法

[](name, *rtype) 点击切换源代码

读取名为 name 的注册表值并返回其值数据。值的类与 read 方法返回的类相同。

如果值类型为 REG_EXPAND_SZ,则返回已替换环境变量的值数据。如果值类型既不是 REG_SZ、REG_MULTI_SZ、REG_DWORD、REG_DWORD_BIG_ENDIAN,也不是 REG_QWORD,则会引发 TypeError。

rtype 的含义与 read 方法相同。

# File win32/lib/win32/registry.rb, line 679
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 #{Registry.type2name(type)} is not supported."
  end
end
[]=(name, rtype, value = nil) 点击切换源代码

将值写入名为 name 的注册表值。

如果指定了 wtype,则值类型为它。否则,值类型取决于值的类::Integer

REG_DWORD

:String

REG_SZ

:Array

REG_MULTI_SZ
# File win32/lib/win32/registry.rb, line 777
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) 点击切换源代码

不允许封送。

# File win32/lib/win32/registry.rb, line 534
def _dump(depth)
  raise TypeError, "can't dump Win32::Registry"
end
close() 点击切换源代码

关闭键。

关闭后,大多数方法会引发错误。

# File win32/lib/win32/registry.rb, line 557
def close
  API.CloseKey(@hkey)
  @hkey = @parent = @keyname = nil
  @hkeyfinal[0] = nil
end
create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk) 点击切换源代码

Win32::Registry.create (self, subkey, desired, opt) 相同。

# File win32/lib/win32/registry.rb, line 548
def create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk)
  self.class.create(self, subkey, desired, opt, &blk)
end
created?() 点击切换源代码

返回键是否已创建(**新创建**)。(参见 Registry.create) - 基本上,您调用 create,然后在您调用 created? 时,返回的实例将告知您是否成功。

# File win32/lib/win32/registry.rb, line 504
def created?
  @disposition == REG_CREATED_NEW_KEY
end
delete(name)
别名:delete_value
delete_key(name, recursive = false) 点击切换源代码

删除名为 name 的子键及其所有值。

如果 recursive 为 false,则子键不能有子键。否则,此方法将递归删除所有子键和值。

# File win32/lib/win32/registry.rb, line 840
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) 点击切换源代码

删除名为 name 的注册表值。我们不能删除“默认”值。

# File win32/lib/win32/registry.rb, line 829
def delete_value(name)
  API.DeleteValue(@hkey, name)
end
也称为:delete
each()
别名:each_value
each_key() { |subkey, wtime| ... } 点击切换源代码

枚举子键。

subkey 是包含子键名称的字符串。wtime 是 FILETIME(64 位整数)形式的最后写入时间。(参见 Registry.wtime2time

# File win32/lib/win32/registry.rb, line 603
def each_key
  index = 0
  while true
    begin
      subkey, wtime = API.EnumKey(@hkey, index)
    rescue Error
      break
    end
    subkey = export_string(subkey)
    yield subkey, wtime
    index += 1
  end
  index
end
each_value() { |subkey, type, data| ... } 点击切换源代码

枚举值。

# File win32/lib/win32/registry.rb, line 566
def each_value
  index = 0
  while true
    begin
      subkey = API.EnumValue(@hkey, index)
    rescue Error
      break
    end
    subkey = export_string(subkey)
    begin
      type, data = read(subkey)
    rescue Error
    else
      yield subkey, type, data
    end
    index += 1
  end
  index
end
也称为:each
flush() 点击切换源代码

将所有属性写入注册表文件。

# File win32/lib/win32/registry.rb, line 865
def flush
  API.FlushKey @hkey
end
info() 点击切换源代码

返回键信息,以数组形式表示::num_keys

The number of subkeys.

:max_key_length

Maximum length of name of subkeys.

:num_values

The number of values.

:max_value_name_length

Maximum length of name of values.

:max_value_length

Maximum length of value of values.

:descriptor_length

Length of security descriptor.

:wtime

Last write time as FILETIME(64-bit integer)

有关详细信息,请参阅 RegQueryInfoKey Win32 API

# File win32/lib/win32/registry.rb, line 888
def info
  API.QueryInfoKey(@hkey)
end
inspect() 点击切换源代码
# File win32/lib/win32/registry.rb, line 527
def inspect
  "\#<Win32::Registry key=#{name.inspect}>"
end
keys() 点击切换源代码

以数组形式返回键。

# File win32/lib/win32/registry.rb, line 621
def keys
  keys_ary = []
  each_key { |key,| keys_ary << key }
  keys_ary
end
name() 点击切换源代码

键的完整路径,例如“HKEY_CURRENT_USERSOFTWAREfoobar”。

# File win32/lib/win32/registry.rb, line 518
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) 点击切换源代码

Win32::Registry.open (self, subkey, desired, opt) 相同。

# File win32/lib/win32/registry.rb, line 541
def open(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk)
  self.class.open(self, subkey, desired, opt, &blk)
end
open?() 点击切换源代码

返回键是否未关闭。

# File win32/lib/win32/registry.rb, line 511
def open?
  !@hkey.nil?
end
read(name, *rtype) 点击切换源代码

读取名为 name 的注册表值,并返回 [type, data] 数组。当 name 为 nil 时,读取“默认”值。type 是值类型。(参见 Win32::Registry::Constants 模块)data 是值数据,其类为::REG_SZ, REG_EXPAND_SZ

String

:REG_MULTI_SZ

Array of String

:REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD

Integer

:REG_BINARY, REG_NONE

String (contains binary data)

当指定 rtype 时,值类型必须包含在 rtype 数组中,否则会引发 TypeError。

# File win32/lib/win32/registry.rb, line 643
def read(name, *rtype)
  type, data = API.QueryValue(@hkey, name)
  unless rtype.empty? or rtype.include?(type)
    raise TypeError, "Type mismatch (expect [#{
      rtype.map{|t|Registry.type2name(t)}.join(', ')}] but #{
      Registry.type2name(type)} present)"
  end
  case type
  when REG_SZ, REG_EXPAND_SZ
    [ type, data.encode(name.encoding, WCHAR).chop ]
  when REG_MULTI_SZ
    [ type, data.encode(name.encoding, WCHAR).split(/\0/) ]
  when REG_BINARY, REG_NONE
    [ 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 #{Registry.type2name(type)} is not supported."
  end
end
read_bin(name) 点击切换源代码

读取名为 name 的 REG_SZ(read_s)、REG_DWORD(read_i) 或 REG_BINARY(read_bin) 注册表值。

如果值类型不匹配,则会引发 TypeError。

# File win32/lib/win32/registry.rb, line 730
def read_bin(name)
  read(name, REG_BINARY)[1]
end
read_i(name) 点击切换源代码

读取名为 name 的 REG_SZ(read_s)、REG_DWORD(read_i) 或 REG_BINARY(read_bin) 注册表值。

如果值类型不匹配,则会引发 TypeError。

# File win32/lib/win32/registry.rb, line 720
def read_i(name)
  read(name, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD)[1]
end
read_s(name) 点击切换源代码

读取名为 name 的 REG_SZ(read_s)、REG_DWORD(read_i) 或 REG_BINARY(read_bin) 注册表值。

如果值类型不匹配,则会引发 TypeError。

# File win32/lib/win32/registry.rb, line 695
def read_s(name)
  read(name, REG_SZ)[1]
end
read_s_expand(name) 点击切换源代码

读取名为 name 的 REG_SZ 或 REG_EXPAND_SZ 注册表值。

如果值类型为 REG_EXPAND_SZ,则会替换环境变量。除非值类型为 REG_SZ 或 REG_EXPAND_SZ,否则会引发 TypeError。

# File win32/lib/win32/registry.rb, line 705
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
values() 点击切换源代码

以数组形式返回值。

# File win32/lib/win32/registry.rb, line 590
def values
  vals_ary = []
  each_value { |*, val| vals_ary << val }
  vals_ary
end
write(name, type, data) 点击切换源代码

将数据写入名为 name 的注册表值。当 name 为 nil 时,写入“默认”值。

类型为类型值。(参见 Registry::Constants 模块)数据类型必须与 read 方法返回的类型相同。

# File win32/lib/win32/registry.rb, line 742
def write(name, type, data)
  termsize = 0
  case type
  when REG_SZ, REG_EXPAND_SZ
    data = data.encode(WCHAR)
    termsize = WCHAR_SIZE
  when REG_MULTI_SZ
    data = data.to_a.map {|s| s.encode(WCHAR)}.join(WCHAR_NUL) << WCHAR_NUL
    termsize = WCHAR_SIZE
  when REG_BINARY, REG_NONE
    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 #{Registry.type2name(type)}"
  end
  API.SetValue(@hkey, name, type, data, data.bytesize + termsize)
end
write_bin(name, value) 点击切换源代码

将值写入名为 name 的注册表值。

值类型为 REG_SZ(write_s)、REG_DWORD(write_i) 或 REG_BINARY(write_bin)。

# File win32/lib/win32/registry.rb, line 821
def write_bin(name, value)
  write name, REG_BINARY, value.to_s
end
write_i(name, value) 点击切换源代码

将值写入名为 name 的注册表值。

值类型为 REG_SZ(write_s)、REG_DWORD(write_i) 或 REG_BINARY(write_bin)。

# File win32/lib/win32/registry.rb, line 811
def write_i(name, value)
  write name, REG_DWORD, value.to_i
end
write_s(name, value) 点击切换源代码

将值写入名为 name 的注册表值。

值类型为 REG_SZ(write_s)、REG_DWORD(write_i) 或 REG_BINARY(write_bin)。

# File win32/lib/win32/registry.rb, line 801
def write_s(name, value)
  write name, REG_SZ, value.to_s
end