Module: Wine

Defined in:
documented/wine.rb

Overview

Module for interacting with WINE registry This module provides methods to get and set values in the WINE registry.

Examples:

Using the Wine module

value = Wine.registry_gets("HKEY_CURRENT_USER\Software\MyApp\Setting")

Constant Summary collapse

BIN =

The path to the WINE binary

$wine_bin
PREFIX =

The path to the WINE prefix

$wine_prefix

Class Method Summary collapse

Class Method Details

.registry_gets(key) ⇒ String, false

Retrieves a value from the WINE registry

Examples:

Getting a registry value

value = Wine.registry_gets("HKEY_CURRENT_USER\Software\MyApp\Setting")

Parameters:

  • key (String)

    The registry key to retrieve the value from

Returns:

  • (String, false)

    The value associated with the key, or false if not found

Raises:

  • (StandardError)

    If there is an error reading the registry



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'documented/wine.rb', line 42

def Wine.registry_gets(key)
  hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme: stupid highlights ]/
  if File.exist?(PREFIX + '/system.reg')
    if hkey == 'HKEY_LOCAL_MACHINE'
      subkey = "[#{subkey.gsub('\\', '\\\\\\')}]"
      if thingie.nil? or thingie.empty?
        thingie = '@'
      else
        thingie = "\"#{thingie}\""
      end
      lookin = result = false
      File.open(PREFIX + '/system.reg') { |f| f.readlines }.each { |line|
        if line[0...subkey.length] == subkey
          lookin = true
        elsif line =~ /^\[/
          lookin = false
        elsif lookin and line =~ /^#{thingie}="(.*)"$/i
          result = $1.split('\\"').join('"').split('\\\\').join('\\').sub(/\\0$/, '')
          break
        end
      }
      return result
    else
      return false
    end
  else
    return false
  end
end

.registry_puts(key, value) ⇒ Boolean

Sets a value in the WINE registry

Examples:

Setting a registry value

success = Wine.registry_puts("HKEY_CURRENT_USER\Software\MyApp\Setting", "NewValue")

Parameters:

  • key (String)

    The registry key to set the value for

  • value (String)

    The value to set

Returns:

  • (Boolean)

    true if the operation was successful, false otherwise

Raises:

  • (StandardError)

    If there is an error writing to the registry



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'documented/wine.rb', line 79

def Wine.registry_puts(key, value)
  hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme ]/
  if File.exist?(PREFIX)
    if thingie.nil? or thingie.empty?
      thingie = '@'
    else
      thingie = "\"#{thingie}\""
    end
    # gsub sucks for this..
    value = value.split('\\').join('\\\\')
    value = value.split('"').join('\"')
    begin
      regedit_data = "REGEDIT4\n\n[#{hkey}\\#{subkey}]\n#{thingie}=\"#{value}\"\n\n"
      filename = "#{TEMP_DIR}/wine-#{Time.now.to_i}.reg"
      File.open(filename, 'w') { |f| f.write(regedit_data) }
      system("#{BIN} regedit #{filename}")
      sleep 0.2
      File.delete(filename)
    rescue
      return false
    end
    return true
  end
end