Class: KtRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ktcommon/ktregistry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(companyName = "kTechSystems") ⇒ KtRegistry

Returns a new instance of KtRegistry.



18
19
20
21
22
23
# File 'lib/ktcommon/ktregistry.rb', line 18

def initialize(companyName="kTechSystems")
  $LOG.debug "KtRegistry::initialize()"
  
  @coName = companyName
  @appName = "Unknown"
end

Instance Attribute Details

#appNameObject

Returns the value of attribute appName.



16
17
18
# File 'lib/ktcommon/ktregistry.rb', line 16

def appName
  @appName
end

#coNameObject

Returns the value of attribute coName.



15
16
17
# File 'lib/ktcommon/ktregistry.rb', line 15

def coName
  @coName
end

Instance Method Details

#deleteValue(valueName) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ktcommon/ktregistry.rb', line 101

def deleteValue(valueName)
  $LOG.debug "KtRegistry::deleteValue( #{valueName} )"
  begin
    Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath(), Win32::Registry::Constants::KEY_WRITE) do |reg|
      reg.delete_value(valueName)
    end
  rescue Win32::Registry::Error => e
    $LOG.error "Error ocurred while deleting value (#{valueName}) from path: #{softwarePath()}"
    $LOG.error "#{e.class} : #{e.to_s}"
    return false
  end
  
  return true
end

#readValue(valueName, defaultValue) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ktcommon/ktregistry.rb', line 31

def readValue(valueName, defaultValue)
  $LOG.debug "KtRegistry::readValue( #{valueName}, #{defaultValue} )"
  result = ""
  regType = nil
  regValue = nil
  
  begin
    Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath()) do |reg|
      #default_reg_typ, reg_defaultVal = reg.read('')   # Throws error if value doesn't exist.
      regType, regValue = reg.read(valueName)
      if(defaultValue.class == TrueClass || defaultValue.class == FalseClass) # Handle returning bools.
        if(regValue != 0)
          regValue = true
        else
          regValue = false
        end
      end
    end
  rescue Win32::Registry::Error => e
    $LOG.error "Error ocurred while reading path: #{softwarePath()}, #{valueName}"
    $LOG.error "#{e.class} : #{e.to_s}"
    $LOG.debug "Returning user supplied default value."
    regValue = defaultValue
  end

  regValue
end

#softwarePathObject



26
27
28
# File 'lib/ktcommon/ktregistry.rb', line 26

def softwarePath()
  path = 'Software' + "\\" + @coName + "\\" + @appName
end

#writeValue(valueName, value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ktcommon/ktregistry.rb', line 60

def writeValue(valueName, value)
  $LOG.debug "KtRegistry::setValue( #{valueName}, #{value} )"
  $LOG.debug "Value class: #{value.class}"
  regType = nil
  regValue = nil
  
  begin
    Win32::Registry::HKEY_LOCAL_MACHINE.open(softwarePath(), Win32::Registry::Constants::KEY_WRITE) do |reg|
      case value.class.to_s
        when 'Fixnum'
          # Write as a dword
          $LOG.debug "Writing integer to registry"
          reg.write_i(valueName, value)
        
        when 'TrueClass'
          # Write as a dword
          $LOG.debug "Writing TrueClass to registry"
          reg.write_i(valueName, 1)
        
        when 'FalseClass'
          # Write as a dword
          $LOG.debug "Writing FalseClass to registry"
          reg.write_i(valueName, 0)
        
        else
          # Write as a string
          $LOG.debug "Writing String to registry"
          reg.write_s(valueName, value)
        
      end
    end
  rescue Win32::Registry::Error => e
    $LOG.error "Error ocurred while writing path: #{softwarePath()}, #{valueName}"
    $LOG.error "#{e.class} : #{e.to_s}"
    return false
  end
  
  return true
end