Class: Win32::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Constants
Defined in:
lib/win32/registry.rb

Direct Known Subclasses

PredefinedKey

Defined Under Namespace

Modules: API, Constants Classes: Error, PredefinedKey

Constant Summary collapse

@@type2name =
{ }
@@final =

finalizer

proc { |hkey| proc { API.CloseKey(hkey[0]) if hkey[0] } }

Constants included from Constants

Constants::HKEY_CLASSES_ROOT, Constants::HKEY_CURRENT_CONFIG, Constants::HKEY_CURRENT_USER, Constants::HKEY_DYN_DATA, Constants::HKEY_LOCAL_MACHINE, Constants::HKEY_PERFORMANCE_DATA, Constants::HKEY_PERFORMANCE_NLSTEXT, Constants::HKEY_PERFORMANCE_TEXT, Constants::HKEY_USERS, Constants::KEY_ALL_ACCESS, Constants::KEY_CREATE_LINK, Constants::KEY_CREATE_SUB_KEY, Constants::KEY_ENUMERATE_SUB_KEYS, Constants::KEY_EXECUTE, Constants::KEY_NOTIFY, Constants::KEY_QUERY_VALUE, Constants::KEY_READ, Constants::KEY_SET_VALUE, Constants::KEY_WRITE, Constants::MAX_KEY_LENGTH, Constants::MAX_VALUE_LENGTH, Constants::REG_BINARY, Constants::REG_CREATED_NEW_KEY, Constants::REG_DWORD, Constants::REG_DWORD_BIG_ENDIAN, Constants::REG_DWORD_LITTLE_ENDIAN, Constants::REG_EXPAND_SZ, Constants::REG_FORCE_RESTORE, Constants::REG_FULL_RESOURCE_DESCRIPTOR, Constants::REG_LEGAL_OPTION, Constants::REG_LINK, Constants::REG_MULTI_SZ, Constants::REG_NONE, Constants::REG_NO_LAZY_FLUSH, Constants::REG_OPENED_EXISTING_KEY, Constants::REG_OPTION_BACKUP_RESTORE, Constants::REG_OPTION_CREATE_LINK, Constants::REG_OPTION_NON_VOLATILE, Constants::REG_OPTION_OPEN_LINK, Constants::REG_OPTION_RESERVED, Constants::REG_OPTION_VOLATILE, Constants::REG_QWORD, Constants::REG_QWORD_LITTLE_ENDIAN, Constants::REG_REFRESH_HIVE, Constants::REG_RESOURCE_LIST, Constants::REG_RESOURCE_REQUIREMENTS_LIST, Constants::REG_SZ, Constants::REG_WHOLE_HIVE_VOLATILE, Constants::STANDARD_RIGHTS_READ, Constants::STANDARD_RIGHTS_WRITE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hkey, parent, keyname, disposition) ⇒ Registry

initialize


563
564
565
566
567
568
569
570
# File '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

Instance Attribute Details

#dispositionObject (readonly)

Returns the value of attribute disposition


571
572
573
# File 'lib/win32/registry.rb', line 571

def disposition
  @disposition
end

#hkeyObject (readonly)

Returns the value of attribute hkey


571
572
573
# File 'lib/win32/registry.rb', line 571

def hkey
  @hkey
end

#keynameObject (readonly)

Returns the value of attribute keyname


571
572
573
# File 'lib/win32/registry.rb', line 571

def keyname
  @keyname
end

#parentObject (readonly)

Returns the value of attribute parent


571
572
573
# File 'lib/win32/registry.rb', line 571

def parent
  @parent
end

Class Method Details

.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) ⇒ Object


541
542
543
544
545
546
547
548
549
550
551
552
553
# File '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) ⇒ Object

utility functions


495
496
497
# File 'lib/win32/registry.rb', line 495

def self.expand_environ(str)
  str.gsub(/%([^%]+)%/) { ENV[$1] || ENV[$1.upcase] || $& }
end

.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) ⇒ Object


526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File '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) ⇒ Object


517
518
519
# File 'lib/win32/registry.rb', line 517

def self.time2wtime(time)
  time.to_i * 10000000 + 116444736000000000
end

.type2name(type) ⇒ Object


509
510
511
# File 'lib/win32/registry.rb', line 509

def self.type2name(type)
  @@type2name[type] || type.to_s
end

.wtime2time(wtime) ⇒ Object


513
514
515
# File 'lib/win32/registry.rb', line 513

def self.wtime2time(wtime)
  Time.at((wtime - 116444736000000000) / 10000000)
end

Instance Method Details

#[](name, *rtype) ⇒ Object


690
691
692
693
694
695
696
697
698
699
700
# File '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) ⇒ Object


746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File '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) ⇒ Object

marshalling

Raises:

  • (TypeError)

600
601
602
# File 'lib/win32/registry.rb', line 600

def _dump(depth)
  raise TypeError, "can't dump Win32::Registry"
end

#closeObject


615
616
617
618
619
# File '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) ⇒ Object


611
612
613
# File '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?Boolean

attributes

Returns:

  • (Boolean)

576
577
578
# File 'lib/win32/registry.rb', line 576

def created?
  @disposition == REG_CREATED_NEW_KEY
end

#delete_key(name, recursive = false) ⇒ Object


784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File '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) ⇒ Object Also known as: delete

delete


779
780
781
# File 'lib/win32/registry.rb', line 779

def delete_value(name)
  API.DeleteValue(@hkey, name)
end

#each_keyObject


644
645
646
647
648
649
650
651
652
653
654
655
656
# File '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_valueObject Also known as: each

iterator


624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File '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

#flushObject

flush


809
810
811
# File 'lib/win32/registry.rb', line 809

def flush
  API.FlushKey @hkey
end

#infoObject

key information


816
817
818
# File 'lib/win32/registry.rb', line 816

def info
  API.QueryInfoKey(@hkey)
end

#inspectObject


593
594
595
# File 'lib/win32/registry.rb', line 593

def inspect
  "\#<Win32::Registry key=#{name.inspect}>"
end

#keysObject


658
659
660
661
662
# File 'lib/win32/registry.rb', line 658

def keys
  keys_ary = []
  each_key { |key,| keys_ary << key }
  keys_ary
end

#nameObject


584
585
586
587
588
589
590
591
# File '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) ⇒ Object

open/close


607
608
609
# File '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?Boolean

Returns:

  • (Boolean)

580
581
582
# File 'lib/win32/registry.rb', line 580

def open?
  !@hkey.nil?
end

#read(name, *rtype) ⇒ Object

reader


667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File '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) ⇒ Object


719
720
721
# File 'lib/win32/registry.rb', line 719

def read_bin(name)
  read(name, REG_BINARY)[1]
end

#read_i(name) ⇒ Object


715
716
717
# File '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) ⇒ Object


702
703
704
# File 'lib/win32/registry.rb', line 702

def read_s(name)
  read(name, REG_SZ)[1]
end

#read_s_expand(name) ⇒ Object


706
707
708
709
710
711
712
713
# File '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) ⇒ Object

writer


726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/win32/registry.rb', line 726

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) ⇒ Object


772
773
774
# File 'lib/win32/registry.rb', line 772

def write_bin(name, value)
  write name, REG_BINARY, value.to_s
end

#write_i(name, value) ⇒ Object


768
769
770
# File 'lib/win32/registry.rb', line 768

def write_i(name, value)
  write name, REG_DWORD, value.to_i
end

#write_s(name, value) ⇒ Object


764
765
766
# File 'lib/win32/registry.rb', line 764

def write_s(name, value)
  write name, REG_SZ, value.to_s
end