Skip to content

Commit

Permalink
Remove c/winapi and rename some windows types (#5448)
Browse files Browse the repository at this point in the history
winapi.cr used old and outdated conventions regarding the windows porting
effort. It is deleted, and the definitions in it which are used are moved to
files corresponding to the windows header they are defined in.

Some windows types are also renamed to retain their capitalisation from
Microsoft's headers.
  • Loading branch information
RX14 committed Jan 2, 2018
1 parent f6e7c2e commit 78ccf91
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 241 deletions.
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/int_safe.cr
@@ -0,0 +1,3 @@
lib LibC
alias DWORD = UInt32
end
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/win_nt.cr
@@ -0,0 +1,3 @@
lib LibC
alias LPSTR = Char*
end
239 changes: 0 additions & 239 deletions src/lib_c/x86_64-windows-msvc/c/winapi.cr

This file was deleted.

16 changes: 16 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/winbase.cr
@@ -0,0 +1,16 @@
require "c/win_nt"
require "c/int_safe"

lib LibC
fun GetLastError : DWORD

FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100_u32
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200_u32
FORMAT_MESSAGE_FROM_STRING = 0x00000400_u32
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800_u32
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000_u32
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000_u32

fun FormatMessageA(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPSTR, nSize : DWORD, arguments : Void*) : DWORD
end
6 changes: 4 additions & 2 deletions src/winerror.cr
@@ -1,13 +1,15 @@
require "c/winbase"

class WinError < Errno
# NOTE: `get_last_error` must be called BEFORE an instance of this class
# is malloced as it would change the "last error" to SUCCESS
def self.new(message)
new(message, LibC._GetLastError)
new(message, LibC.GetLastError)
end

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

0 comments on commit 78ccf91

Please sign in to comment.