Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou1okada/9613061 to your computer and use it in GitHub Desktop.
Save kou1okada/9613061 to your computer and use it in GitHub Desktop.
Monkey patch for 'win32/registry' of Ruby.
begin
Win32::Registry::Error.new(259)
rescue ArgumentError => e
if e.message == "invalid byte sequence in UTF-8"
class Win32::Registry::Error
FormatMessageW = Kernel32.extern "int FormatMessageW(int, void *, int, int, void *, int, void *)", :stdcall
def initialize(code)
@code = code
msg = "\0\0".force_encoding(Encoding::UTF_16LE) * 1024
len = FormatMessageW.call(0x1200, 0, code, 0, msg, msg.size, 0)
msg = msg[0, len].encode(Encoding.find(Encoding.locale_charmap))
super msg.tr("\r".encode(msg.encoding), '').chomp
end
end
else
raise e
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment