Working with the registry

I found:

require ‘win32/registry’
keyname = ‘SYSTEM\CurrentControlSet\Control\Session Manager
\Environment’
access = Win32::Registry::KEY_ALL_ACCESS
Win32::Registry::HKEY_LOCAL_MACHINE.open(keyname, access) do |reg|

Add to path

[‘c:\foo\bin’, ‘c:\bar\bin’].each do |directory|
unless (reg[‘path’].include? directory)
reg[‘path’] += ‘;’ + directory
end
end

Add environment variable

reg[‘foobar’] = ‘42’

Note: won’t take effect until restart!

end

<http://multipart-mixed.com/downloads/software/
spectra_d500_lessons_learned.pdf>

but would like to query the registry from ruby. Just thinking outloud
here, haven’t found a tutorial invovling the registry and ruby which I
find useful yet :frowning:

C:\msi>
C:\msi>
C:\msi>dir
Volume in drive C has no label.
Volume Serial Number is 0491-510F

Directory of C:\msi

12/06/2007 12:36 PM .
12/06/2007 12:36 PM …
12/06/2007 12:25 PM 10,970,624 python-2.5.1.msi
1 File(s) 10,970,624 bytes
2 Dir(s) 28,396,879,872 bytes free

C:\msi>
C:\msi>reg query HKEY_LOCAL_MACHINE\Software\Policies\Microsoft
\Windows
\Installe
r

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\Installer
EnableAdminTSRemote REG_DWORD 0x1

C:\msi>
C:\msi>reg query HKEY_CURRENT_USER\Software\Policies\Microsoft
\Windows
\

! REG.EXE VERSION 3.0

HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\

HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Control Panel

HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\System

C:\msi>

thanks,

Thufir

On Dec 7, 7:15 pm, Thufir [email protected] wrote:

but would like to query the registry from ruby. Just thinking outloud
here, haven’t found a tutorial invovling the registry and ruby which I
find useful yet :frowning:

The following is what I was able to figure out from the standard
library API docs at http://www.ruby-doc.org/stdlib/. It’s pretty
Rubyish once you get going.

C:\msi>reg query HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\Installer

[in irb]
irb> require ‘win32/registry’
irb> include Win32

irb> Registry.open(Registry::HKEY_LOCAL_MACHINE, ‘Software\Policies
\Microsoft\Windows\Installer’).each_value do |subkey, type, data|
irb* puts “#{subkey} (#{type}): #{data}”
irb> end

The only problem I see with this is that you get the constant value
for type (4 in this case) rather than the constant name (REG_DWORD).
Is that what you were wanting to do?

C:\msi>reg query HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows

I don’t have this key on my machine. Only HKEY_CURRENT_USER\Software
\Policies\Microsoft\SystemCertificates which has subkeys, but no
values, but all keys should be similar to the above.

I would like to use:

each_value() {|subkey, type, data| …}

iterator

[Source]

File Win32API/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

<http://www.ruby-doc.org/stdlib/libdoc/Win32API/rdoc/classes/Win32/
Registry.html>

here’s what I have:

irb(main):040:3*
irb(main):041:3*
puts.each_key(Registry.open(Registry::HKEY_LOCAL_MACHINE, ‘Software
\Policies’))
irb(main):042:5’ ’
irb(main):043:5>
irb(main):044:5*

But I seem to be having some sort of problem with closing quotes
(syntax). Can I do that with the puts method?

thanks,

Thufir

On Dec 12, 2:29 pm, Thufir [email protected] wrote:

But I seem to be having some sort of problem with closing quotes
(syntax).

nickname = ‘Gavin ‘Phrogz’ Kistner’

The backslashes are escaping your last quote. Instead try:
‘Software\Policies\’
(use a backslash to escape the backslash)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs