レジストリから値を取得

「Excite を利用して英語=>日本語翻訳」 で、
会社から使用する場合にプロキシを介すのでアクセスできない。
インターネットオプションの設定を取得できると嬉しいので
レジストリから取れないか調べたところ
Win32::Registry を使用すれば良い事が分かったので使ってみる。
レジストリからの取得って初めての経験だなぁ。


これ↓だけで取得できるようです。

key = 'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
reg = Win32::Registry::HKEY_CURRENT_USER.open(key)
if reg["ProxyEnable"] == 1
  option[:proxy] = "http://" + reg["ProxyServer"]
 end

今回は取得するだけだけど書き込みもできるみたい。


で、バージョンアップしてみる。
DL先

#!ruby -Ks
require 'vr/vrcontrol'
require 'vr/vrlayout'
require 'vr/clipboard'
require 'open-uri'
require 'uri'
require 'win32/registry'

# Version情報
# 0.1 2006.11.05 initialize
# 0.2 2006.11.06 インターネットオプションのプロキシ設定を見る
VER = 0.2

class MyForm < VRForm
  include VRGridLayoutManager
  WEB_URL = "http://www.excite.co.jp/world/english/?wb_lp=ENJA&before="

  CAPTION = "English to Japanese - V#{VER}"
  # コンストラクタ
  def construct
    self.caption = CAPTION
    setDimension(10,20)
    addControl VRText,"text_before","", 0,0,10,9,WStyle::WS_VSCROLL
    addControl VRButton,"button_change","", 0,9,10,2
    addControl VRText,"text_after", "",0,11,10,9,WStyle::WS_VSCROLL 
    @button_change.caption = "English => Japanese"
    # proxy情報など
    @option = createOption
  end
  # 変換ボタンがおされた
  def button_change_clicked

    @text_after.text = "・・・ データ取得中 ・・・"

    Thread.start {
      begin
        url = WEB_URL + URI.escape(@text_before.text)
        self.caption = CAPTION + " : " + url
        start_time = Time.now
        res = open(url, @option)
        time = Time.now - start_time
        res.read =~ /(.*?)<\/textarea>/m
        @text_after.text = $1 + " \n応答時間 : " + time.to_s + "[s]"
        res.close
      rescue => e
        msg = "ERROR!!\n"  + e.to_s + "\n"
        msg << "OPTION : \n"
        @option.each{
          |key,value| msg << key.to_s + " => " + value.to_s + "\n"
        }
        @text_after.text = msg
      end
    }
  end

  private
  def createOption
    option = Hash.new
    key = 'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
    reg = Win32::Registry::HKEY_CURRENT_USER.open(key)
    if reg["ProxyEnable"] == 1
      option[:proxy] = "http://" + reg["ProxyServer"]
    end
    return option
  end
end

VRLocalScreen.showForm(MyForm,100,100,450,300)
VRLocalScreen.messageloop