Skip to content

Commit

Permalink
Change WinError to get message as UTF16 string (#6442)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and RX14 committed Jul 28, 2018
1 parent 87781a9 commit 97344a3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/lib_c/x86_64-windows-msvc/c/winbase.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ lib LibC
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000_u32
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000_u32

# TODO: Use LPWSTR
fun FormatMessageA(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPSTR, nSize : DWORD, arguments : Void*) : DWORD
fun FormatMessageW(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPWSTR, nSize : DWORD, arguments : Void*) : DWORD

struct FILETIME
dwLowDateTime : DWORD
Expand Down
6 changes: 3 additions & 3 deletions src/winerror.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class WinError < Errno
end

def initialize(message, code)
buffer = uninitialized UInt8[256]
size = LibC.FormatMessageA(LibC::FORMAT_MESSAGE_FROM_SYSTEM, nil, code, 0, buffer, buffer.size, nil)
details = String.new(buffer.to_unsafe, size).strip
buffer = uninitialized UInt16[256]
size = LibC.FormatMessageW(LibC::FORMAT_MESSAGE_FROM_SYSTEM, nil, code, 0, buffer, buffer.size, nil)
details = String.from_utf16(buffer.to_slice[0, size]).strip
super("#{message}: [WinError #{code}, #{details}]", winerror_to_errno(code))
end

Expand Down

0 comments on commit 97344a3

Please sign in to comment.