Saturday, April 3, 2010

FXRegistry and the Windows Registry

Someone pointed me to the FXRegistry class when I was faced with the problem of trying to save/retrieve settings from the FXRuby app I'm writing.

I read the FXRuby API docs but they didn't tell me much.  An internet search brought me to the Fox-Toolkit.org documentation page, and while more informative, I still didn't have clear examples of how to do this.

Turns out the FXRuby User's Guide Examples Page includes a program (imageviewer.rb) that uses FXRegistry to save/retrieve some settings.  That was beginning to help but I still had lots of questions. I found another example in chapter 12 of the FXRuby book ("FXRuby: Create Lean and Mean GUIs with Ruby" by Lyle Johnson). Section 12.3 talks about resizing layouts and I didn't care for that, but I did care about the fact that it saves and reads the settings from the registry.  The code for splitter.rb is available online. (NOTE: this splitter.rb app is different from the splitter.rb app you find in the FXRuby examples page and directory on your hard drive.)

What did I learn from all this?  Settings are magically saved somewhere, but no one could tell me exactly where in MS Windows. (I'm currently running/testing this on Windows XP.)  So I opened up 'regedit', ran the FXRuby apps and looked for the saved settings.

Here's what I found.

The 'splitter.rb' app saved the settings in: \HKEY_CURRENT_USER\Software\FoxDefault\Application\Settings

The 'imageviewer.rb' app saved the settings in: \HKEY_CURRENT_USER\Software\FoxTest\ImageViewer\...

Okay. So, I copied the registry commands from imageviewer.rb to my test app and ran it.  Where did the settings get saved?  Turns out they were saved in the same location as the splitter.rb.  Why? How do I change that?  How do I control where the settings go?

I noticed that there were other settings under the 'FoxTest' key in the Registry.  I ran a lot of sample code that I found on the net, and several of them must have written to the registry.

I stared back and forth at the code for the 3 apps (mine, splitter and imageviewer) until it hit me - I found the difference and the answer I was looking for!

If you look at the bottom of the imageviewer code, you will see the following line:

  application = FXApp.new("ImageViewer", "FoxTest")

When I look at the splitter code, it looks slightly different:

  FXApp.new do |app|
    SplitterExample.new(app)
    app.create
    app.run
  end

My source code looked like:

  application = FXApp.new

Aha! There it is!

The imageviewer code specifies the application name in the 'FXApp.new' creation line and that is used in the FXRegistry key names.  The splitter code and my code didn't specify any names so it gets saved to "FoxDefault\Application".

So, I changed my code to now read:

  application = FXApp.new( "TestApp", "FoxTest")

And I found the settings saved in: \HKEY_CURRENT_USER\Software\FoxTest\TestApp\

Yay!  I now know where the FXRegistry settings are saved in Windows Registry and how to change the location.  Cool.

No comments:

Post a Comment