[前][次][番号順一覧][スレッド一覧]

ruby-changes:29786

From: nobu <ko1@a...>
Date: Mon, 8 Jul 2013 23:37:01 +0900 (JST)
Subject: [ruby-changes:29786] nobu:r41838 (trunk): win32/registry.rb: use WCHAR

nobu	2013-07-08 23:36:48 +0900 (Mon, 08 Jul 2013)

  New Revision: 41838

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41838

  Log:
    win32/registry.rb: use WCHAR
    
    * ext/dl/win32/lib/win32/registry.rb (Error, API): use WCHAR
      interfaces.  c.f. [Bug #8508]

  Modified files:
    trunk/ChangeLog
    trunk/ext/dl/win32/lib/win32/registry.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41837)
+++ ChangeLog	(revision 41838)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul  8 23:36:45 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/dl/win32/lib/win32/registry.rb (Error, API): use WCHAR
+	  interfaces.  c.f. [Bug #8508]
+
 Mon Jul  8 23:13:11 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* win32/win32.c (rb_w32_pow): move from win32.h and disable strict
Index: ext/dl/win32/lib/win32/registry.rb
===================================================================
--- ext/dl/win32/lib/win32/registry.rb	(revision 41837)
+++ ext/dl/win32/lib/win32/registry.rb	(revision 41838)
@@ -63,6 +63,10 @@ For detail, see the MSDN[http://msdn.mic https://github.com/ruby/ruby/blob/trunk/ext/dl/win32/lib/win32/registry.rb#L63
 
 =end rdoc
 
+  WCHAR = Encoding::UTF_16LE
+  WCHAR_SPACE = "\0".encode(WCHAR).freeze
+  LOCALE = Encoding.find(Encoding.locale_charmap)
+
   class Registry
 
     #
@@ -164,12 +168,12 @@ For detail, see the MSDN[http://msdn.mic https://github.com/ruby/ruby/blob/trunk/ext/dl/win32/lib/win32/registry.rb#L168
         extend DL::Importer
         dlload "kernel32.dll"
       end
-      FormatMessageA = Kernel32.extern "int FormatMessageA(int, void *, int, int, void *, int, void *)", :stdcall
+      FormatMessageW = Kernel32.extern "int FormatMessageW(int, void *, int, int, void *, int, void *)", :stdcall
       def initialize(code)
         @code = code
-        msg = "\0".force_encoding(Encoding::ASCII_8BIT) * 1024
-        len = FormatMessageA.call(0x1200, 0, code, 0, msg, 1024, 0)
-        msg = msg[0, len].force_encoding(Encoding.find(Encoding.locale_charmap))
+        msg = WCHAR_SPACE * 1024
+        len = FormatMessageW.call(0x1200, 0, code, 0, msg, 1024, 0)
+        msg = msg[0, len].encode
         super msg.tr("\r", '').chomp
       end
       attr_reader :code
@@ -209,12 +213,12 @@ For detail, see the MSDN[http://msdn.mic https://github.com/ruby/ruby/blob/trunk/ext/dl/win32/lib/win32/registry.rb#L213
       extend DL::Importer
       dlload "advapi32.dll"
       [
-        "long RegOpenKeyExA(void *, void *, long, long, void *)",
-        "long RegCreateKeyExA(void *, void *, long, long, long, long, void *, void *, void *)",
-        "long RegEnumValueA(void *, long, void *, void *, void *, void *, void *, void *)",
-        "long RegEnumKeyExA(void *, long, void *, void *, void *, void *, void *, void *)",
-        "long RegQueryValueExA(void *, void *, void *, void *, void *, void *)",
-        "long RegSetValueExA(void *, void *, long, long, void *, long)",
+        "long RegOpenKeyExW(void *, void *, long, long, void *)",
+        "long RegCreateKeyExW(void *, void *, long, long, long, long, void *, void *, void *)",
+        "long RegEnumValueW(void *, long, void *, void *, void *, void *, void *, void *)",
+        "long RegEnumKeyExW(void *, long, void *, void *, void *, void *, void *, void *)",
+        "long RegQueryValueExW(void *, void *, void *, void *, void *, void *)",
+        "long RegSetValueExW(void *, void *, long, long, void *, long)",
         "long RegDeleteValue(void *, void *)",
         "long RegDeleteKey(void *, void *)",
         "long RegFlushKey(void *)",
@@ -251,52 +255,58 @@ For detail, see the MSDN[http://msdn.mic https://github.com/ruby/ruby/blob/trunk/ext/dl/win32/lib/win32/registry.rb#L255
 
       def OpenKey(hkey, name, opt, desired)
         result = packdw(0)
-        check RegOpenKeyExA.call(hkey, name, opt, desired, result)
+        check RegOpenKeyExW.call(hkey, name.encode(WCHAR), opt, desired, result)
         unpackdw(result)
       end
 
       def CreateKey(hkey, name, opt, desired)
         result = packdw(0)
         disp = packdw(0)
-        check RegCreateKeyExA.call(hkey, name, 0, 0, opt, desired,
+        check RegCreateKeyExW.call(hkey, name.encode(WCHAR), 0, 0, opt, desired,
                                    0, result, disp)
         [ unpackdw(result), unpackdw(disp) ]
       end
 
       def EnumValue(hkey, index)
-        name = ' ' * Constants::MAX_KEY_LENGTH
+        name = WCHAR_SPACE * Constants::MAX_KEY_LENGTH
         size = packdw(Constants::MAX_KEY_LENGTH)
-        check RegEnumValueA.call(hkey, index, name, size, 0, 0, 0, 0)
-        name[0, unpackdw(size)]
+        check RegEnumValueW.call(hkey, index, name, size, 0, 0, 0, 0)
+        name[0, unpackdw(size)].encode
       end
 
       def EnumKey(hkey, index)
-        name = ' ' * Constants::MAX_KEY_LENGTH
+        name = WCHAR_SPACE * Constants::MAX_KEY_LENGTH
         size = packdw(Constants::MAX_KEY_LENGTH)
         wtime = ' ' * 8
-        check RegEnumKeyExA.call(hkey, index, name, size, 0, 0, 0, wtime)
-        [ name[0, unpackdw(size)], unpackqw(wtime) ]
+        check RegEnumKeyExW.call(hkey, index, name, size, 0, 0, 0, wtime)
+        [ name[0, unpackdw(size)].encode, unpackqw(wtime) ]
       end
 
       def QueryValue(hkey, name)
         type = packdw(0)
         size = packdw(0)
-        check RegQueryValueExA.call(hkey, name, 0, type, 0, size)
-        data = ' ' * unpackdw(size)
-        check RegQueryValueExA.call(hkey, name, 0, type, data, size)
-        [ unpackdw(type), data[0, unpackdw(size)] ]
+        name = name.encode(WCHAR)
+        check RegQueryValueExW.call(hkey, name, 0, type, 0, size)
+        data = WCHAR_SPACE * unpackdw(size)
+        check RegQueryValueExW.call(hkey, name, 0, type, data, size)
+        [ unpackdw(type), data[0, unpackdw(size)].encode ]
       end
 
       def SetValue(hkey, name, type, data, size)
-        check RegSetValueExA.call(hkey, name, 0, type, data, size)
+        case type
+        when REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ
+          data = data.encode(WCHAR)
+          size ||= data.size + 1
+        end
+        check RegSetValueExW.call(hkey, name.encode(WCHAR), 0, type, data, size)
       end
 
       def DeleteValue(hkey, name)
-        check RegDeleteValue.call(hkey, name)
+        check RegDeleteValue.call(hkey, name.encode(WCHAR))
       end
 
       def DeleteKey(hkey, name)
-        check RegDeleteKey.call(hkey, name)
+        check RegDeleteKey.call(hkey, name.encode(WCHAR))
       end
 
       def FlushKey(hkey)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]