Thursday, November 29, 2012

Easy Windows registry editing with JRuby


DSCF0434 by rabbit57i from flickr (CC-NC-ND)

Like all people who came out of Unix tradition I approached Windows registry as something not to be touched even with a long stick, but it turned out not to be that bad.

The first thing you need to know about Windows registry is that it has multiple roots. All our viewing and editing will apply to particular key only (usually HKEY_LOCAL_MACHINE).

It looks like a pretty stupid decision, but then it comes from people who use C: D: etc. instead of single directory tree.

  require "win32/registry"
  def hklm
    Win32::Registry::HKEY_LOCAL_MACHINE
  end
Reading information from registry, like installation paths of various programs, is very easy:

  hklm.open('SOFTWARE\Wow6432Node\SEGA\Medieval II Total War')["AppPath"] rescue nil
  hklm.open('SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 4700')["InstallLocation"] rescue nil
If you want nil instead of exception just rescue nil the entire thing.

Writing to registry is very easy as well, here's actual example:

    cv = hklm.create('SOFTWARE\Wow6432Node\SEGA\Medieval II Total War\Mods\Unofficial\Concentrated Vanilla')
    cv["Author"]="Tomasz Wegrzanowski"
    cv["ConfigFile"]="concentrated_vanilla.cfg"
    cv["DisplayName"]="Concentrated Vanilla"
    cv["FullName"]="Concentrated Vanilla"
    cv["Language"]="english"
    cv["Path"]="mods/concentrated_vanilla"
    cv["Version"]="0.60"
    cv["GameExe"]="medieval2.exe"
And deleting things to uninstall:

    hklm.delete_key('SOFTWARE\Wow6432Node\SEGA\Medieval II Total War\Mods\Unofficial\Concentrated Vanilla', true)

And that's about it. If you want to explore the registry either start regedit program, or start jirb and play with it from JRuby REPL.

No comments:

Post a Comment