Last active
August 29, 2015 13:57
-
-
Save kou1okada/9613061 to your computer and use it in GitHub Desktop.
Monkey patch for 'win32/registry' of Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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