Registry – orzFly https://oldblog.orzfly.com His Memories from Here to There, with Fuzzy Ginkgo Sat, 11 Feb 2017 13:57:34 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.5 RGSS 中读取注册表 https://oldblog.orzfly.com/html/rgss-registry.html Sun, 26 Aug 2012 04:45:23 +0000 http://orzfly.com/?p=219 有了这段脚本,便可以在 RGSS 中使用一种较为简单的语法来获得注册表中的字符串值,如:Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]
因为 Azoki 中用到了这个,于是先发出来最简单的部分好了。
这段脚本依赖于 String#s2u。

#==============================================================================
# ■ Registry
#------------------------------------------------------------------------------
#  极其简单的一个只能读取字符串值的注册表读取模块。
#------------------------------------------------------------------------------
=begin
  来源:http://orzfly.com/html/rgss-registry.html
  依赖:http://orzfly.com/html/rgss-string-encoding.html
  使用举例: 
    Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]
    读取 HKEY_LOCAL_MACHINESoftwareEnterbrainRGSSRTP 下的 Standard 的值
=end
#==============================================================================
module Registry
  #--------------------------------------------------------------------------
  # ● API 定义
  #--------------------------------------------------------------------------
  module NativeMethods
    RegOpenKey = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
    RegQueryValueEx = Win32API.new('advapi32', 'RegQueryValueExA', 'LPLPPP', 'L')
    RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
  end
  
  #--------------------------------------------------------------------------
  # ● 根代理
  #--------------------------------------------------------------------------
  class RegistryRoot
    def initialize root
      @root = root
    end
    
    def [] key
      RegistryKey.new @root, key
    end
  end
  
  #--------------------------------------------------------------------------
  # ● 键代理
  #--------------------------------------------------------------------------
  class RegistryKey
    include NativeMethods
    
    def initialize root, key
      @root, @key = root, key
    end
    
    def [] name
      begin
        key = 0.chr * 4

        RegOpenKey.call(@root, @key, 0, 0x20019, key)
        key = key.unpack('l')[0]
        type = 0.chr * 4
        size = 0.chr * 4

        RegQueryValueEx.call(key, name, 0, type, 0, size)
        data = ' ' * size.unpack('l')[0]

        RegQueryValueEx.call(key, name, 0, type, data, size)
        value = data.respond_to?(:force_encoding) ? 
          data.force_encoding("ascii-8bit").chop.s2u :
          data.chop.s2u
        RegCloseKey.call(key)
        
        return value
      rescue
        nil
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # ● 常量定义
  #--------------------------------------------------------------------------
  HKEY_CLASSES_ROOT  = HKCR = RegistryRoot.new(0x80000000)
  HKEY_CURRENT_USER  = HKCU = RegistryRoot.new(0x80000001)
  HKEY_LOCAL_MACHINE = HKLM = RegistryRoot.new(0x80000002)
end

]]>