Class: Win32::Registry
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Constants
- Defined in:
- lib/win32/registry.rb
Defined Under Namespace
Modules: API, Constants
Classes: Error, PredefinedKey
Constant Summary
collapse
- @@type2name =
{ }
- @@final =
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
-
#[](name, *rtype) ⇒ Object
-
#[]=(name, rtype, value = nil) ⇒ Object
-
#_dump(depth) ⇒ Object
-
#close ⇒ Object
-
#create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk) ⇒ Object
-
#created? ⇒ Boolean
-
#delete_key(name, recursive = false) ⇒ Object
-
#delete_value(name) ⇒ Object
(also: #delete)
-
#each_key ⇒ Object
-
#each_value ⇒ Object
(also: #each)
-
#flush ⇒ Object
-
#info ⇒ Object
-
#initialize(hkey, parent, keyname, disposition) ⇒ Registry
constructor
-
#inspect ⇒ Object
-
#keys ⇒ Object
-
#name ⇒ Object
-
#open(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk) ⇒ Object
-
#open? ⇒ Boolean
-
#read(name, *rtype) ⇒ Object
-
#read_bin(name) ⇒ Object
-
#read_i(name) ⇒ Object
-
#read_s(name) ⇒ Object
-
#read_s_expand(name) ⇒ Object
-
#write(name, type, data) ⇒ Object
-
#write_bin(name, value) ⇒ Object
-
#write_i(name, value) ⇒ Object
-
#write_s(name, value) ⇒ Object
Constructor Details
#initialize(hkey, parent, keyname, disposition) ⇒ Registry
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
#disposition ⇒ Object
Returns the value of attribute disposition
571
572
573
|
# File 'lib/win32/registry.rb', line 571
def disposition
@disposition
end
|
#hkey ⇒ Object
Returns the value of attribute hkey
571
572
573
|
# File 'lib/win32/registry.rb', line 571
def hkey
@hkey
end
|
#keyname ⇒ Object
Returns the value of attribute keyname
571
572
573
|
# File 'lib/win32/registry.rb', line 571
def keyname
@keyname
end
|
#parent ⇒ Object
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
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
#[]=(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
600
601
602
|
# File 'lib/win32/registry.rb', line 600
def _dump(depth)
raise TypeError, "can't dump Win32::Registry"
end
|
#close ⇒ Object
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
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) end
end
|
#delete_value(name) ⇒ Object
Also known as:
delete
779
780
781
|
# File 'lib/win32/registry.rb', line 779
def delete_value(name)
API.DeleteValue(@hkey, name)
end
|
#each_key ⇒ Object
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_value ⇒ Object
Also known as:
each
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
|
#flush ⇒ Object
809
810
811
|
# File 'lib/win32/registry.rb', line 809
def flush
API.FlushKey @hkey
end
|
#info ⇒ Object
816
817
818
|
# File 'lib/win32/registry.rb', line 816
def info
API.QueryInfoKey(@hkey)
end
|
#inspect ⇒ Object
593
594
595
|
# File 'lib/win32/registry.rb', line 593
def inspect
"\#<Win32::Registry key=#{name.inspect}>"
end
|
#keys ⇒ Object
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
|
#name ⇒ Object
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
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
580
581
582
|
# File 'lib/win32/registry.rb', line 580
def open?
!@hkey.nil?
end
|
#read(name, *rtype) ⇒ Object
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_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
#write(name, type, data) ⇒ Object
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
|