[Win32utils-devel] Ruby 1.9 and Encoding.default_external

Daniel Berger djberg96 at gmail.com
Tue May 22 00:17:19 UTC 2012


Hi,

Just curious, when using Ruby 1.9 on my Windows 7 laptop, strings are
encoded in IBM437 by default. However, when I check my default code
page using GetCPInfoEx, I get Windows-1252.

This is causing some confusion when trying to port code to FFI and
JRuby, which by default encodes strings as Windows-1252.

# code_page.rb
require 'ffi'

class Windows
  extend FFI::Library
  ffi_convention :stdcall
  ffi_lib :kernel32

  attach_function :GetConsoleCP, [], :uint
  attach_function :GetCPInfoEx, :GetCPInfoExA, [:uint, :ulong, :pointer], :bool

  # From WinNls.h
  MAX_LEADBYTES = 12
  MAX_DEFAULTCHAR = 2
  CP_ACP = 0

  # From WinDef.h
  MAX_PATH = 260

  class CPINFOEX < FFI::Struct
    layout(
      :MaxCharSize, :uint,
      :DefaultChar, [:uchar, MAX_DEFAULTCHAR],
      :LeadByte, [:uchar, MAX_LEADBYTES],
      :UnicodeDefaultChar, [:char, 2],
      :CodePage, :uint,
      :CodePageName, [:char, MAX_PATH]
    )
  end

  def self.cp_number
    GetConsoleCP()
  end

  def self.cp_name
    ptr = CPINFOEX.new

    unless GetCPInfoEx(CP_ACP, 0, ptr)
      raise SystemCallError, FFI.errno, "GetCPInfoEx"
    end

    ptr[:CodePageName]
  end
end

p Windows.cp_number # 437
p Windows.cp_name # 1252  (ANSI - Latin I)

Is this a case of the system default not being the same as the console
code page? If so, isn't this a bug in MRI then?

Regards,

Dan


More information about the win32utils-devel mailing list