Skip to content

Commit

Permalink
check successful win32_last_error
Browse files Browse the repository at this point in the history
successful_or_if_not_messageout() returns true
otherwise raise exception or message output to stderr
  • Loading branch information
YO4 committed Oct 20, 2024
1 parent 2561d29 commit 24339e6
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions lib/yamatanooroti/windows/windows-definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ module Yamatanooroti::WindowsDefinition
# BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent, DWORD dwProcessGroupId);
extern 'BOOL GenerateConsoleCtrlEvent(DWORD, DWORD);', :stdcall

private def error_message(r, method_name, exception: true)
return if not r.zero?
private def successful_or_if_not_messageout(r, method_name, exception: true)
return true if not r.zero?
err = Fiddle.win32_last_error
if err == 0
$stderr.puts "Info: #{method_name} returns zero but win32_last_error has successful zero."
return true
end
string = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP, FREE)
n = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
Expand All @@ -229,30 +233,30 @@ module Yamatanooroti::WindowsDefinition
raise msg
else
$stderr.puts msg
return false
end
end

def get_console_screen_buffer_info(handle)
csbi = CONSOLE_SCREEN_BUFFER_INFO.malloc(FREE)
r = GetConsoleScreenBufferInfo(handle, csbi)
error_message(r, 'GetConsoleScreenBufferInfo')
successful_or_if_not_messageout (r, 'GetConsoleScreenBufferInfo')
return csbi
end

def set_console_screen_buffer_info_ex(handle, h, w, buffer_height)
csbi = CONSOLE_SCREEN_BUFFER_INFOEX.malloc(FREE)
csbi.cbSize = CONSOLE_SCREEN_BUFFER_INFOEX.size
r = GetConsoleScreenBufferInfoEx(handle, csbi)
error_message(r, 'GetConsoleScreenBufferSize')
successful_or_if_not_messageout (r, 'GetConsoleScreenBufferSize')
csbi.dwSize_X = w
csbi.dwSize_Y = buffer_height
csbi.Left = 0
csbi.Right = w - 1
csbi.Top = [csbi.Top, buffer_height - h].min
csbi.Bottom = csbi.Top + h - 1
r = SetConsoleScreenBufferInfoEx(handle, csbi)
error_message(r, 'SetConsoleScreenBufferInfoEx')
return r != 0
return successful_or_if_not_messageout (r, 'SetConsoleScreenBufferInfoEx')
end

def set_console_window_info(handle, h, w)
Expand All @@ -262,8 +266,7 @@ def set_console_window_info(handle, h, w)
rect.Right = w - 1
rect.Bottom = h - 1
r = SetConsoleWindowInfo(handle, 1, rect)
error_message(r, 'SetConsoleWindowInfo')
return r != 0
return successful_or_if_not_messageout (r, 'SetConsoleWindowInfo')
end

def set_console_window_size(handle, h, w)
Expand All @@ -290,26 +293,24 @@ def create_console_file_handle(name)
0
)
fh = [fh].pack("J").unpack1("J")
error_message(0, name) if fh == INVALID_HANDLE_VALUE
successful_or_if_not_messageout (0, name) if fh == INVALID_HANDLE_VALUE
fh
end

def close_handle(handle)
r = CloseHandle(handle)
error_message(r, "CloseHandle")
return r != 0
return successful_or_if_not_messageout (r, "CloseHandle")
end

def free_console
r = FreeConsole()
error_message(r, "FreeConsole")
return r != 0
return successful_or_if_not_messageout (r, "FreeConsole")
end

def attach_console(pid = ATTACH_PARENT_PROCESS, maybe_fail: false)
r = AttachConsole(pid)
error_message(r, 'AttachConsole') unless maybe_fail
return r != 0
return successful_or_if_not_messageout (r, 'AttachConsole') unless maybe_fail
nil
end

SHOWWINDOW_MAP = {
Expand Down Expand Up @@ -346,7 +347,7 @@ def create_console(command, show = SW_SHOWNORMAL)
Fiddle::NULL, Fiddle::NULL,
startup_info, console_process_info
)
error_message(r, 'CreateProcessW')
successful_or_if_not_messageout (r, 'CreateProcessW')
end
close_handle(console_process_info.hProcess)
close_handle(console_process_info.hThread)
Expand All @@ -355,7 +356,7 @@ def create_console(command, show = SW_SHOWNORMAL)

def get_std_handle(stdhandle)
fh = GetStdHandle(stdhandle)
error_message(0, name) if fh == INVALID_HANDLE_VALUE
successful_or_if_not_messageout (0, name) if fh == INVALID_HANDLE_VALUE
fh
end

Expand All @@ -377,7 +378,7 @@ def read_console_output(handle, row, width)
buffer = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SHORT * width, FREE)
n = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, FREE)
r = ReadConsoleOutputCharacterW(handle, buffer, width, row << 16, n)
error_message(r, "ReadConsoleOutputCharacterW")
successful_or_if_not_messageout (r, "ReadConsoleOutputCharacterW")
return wc2mb(buffer[0, n.to_str.unpack1("L") * 2]).gsub(/ *$/, "")
end

Expand Down Expand Up @@ -417,23 +418,22 @@ def build_key_input_record(str)
def write_console_input(handle, records, n)
written = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, FREE)
r = WriteConsoleInputW(handle, records, n, written)
error_message(r, 'WriteConsoleInput')
successful_or_if_not_messageout (r, 'WriteConsoleInput')
return written.to_str.unpack1('L')
end

def get_number_of_console_input_events(handle)
n = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, FREE)
r = GetNumberOfConsoleInputEvents(handle, n)
error_message(r, 'GetNumberOfConsoleInputEvents')
successful_or_if_not_messageout (r, 'GetNumberOfConsoleInputEvents')
return n.to_str.unpack1('L')
end

def get_console_mode(handle)
mode = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DWORD, FREE)
mode[0, Fiddle::SIZEOF_DWORD] = "\0".b * Fiddle::SIZEOF_DWORD
r = GetConsoleMode(handle, mode)
error_message(r, 'GetConsoleMode', exception: false)
mode.to_str.unpack1('L')
successful_or_if_not_messageout (r, 'GetConsoleMode', exception: false) ? mode.to_str.unpack1('L') : nil
end

def set_console_mode(handle, mode)
Expand All @@ -442,12 +442,12 @@ def set_console_mode(handle, mode)

def set_console_codepage(cp)
r = SetConsoleCP(cp)
error_message(r, 'SetConsoleCP', exception: false)
return successful_or_if_not_messageout (r, 'SetConsoleCP', exception: false)
end

def set_console_output_codepage(cp)
r = SetConsoleOutputCP(cp)
error_message(r, 'SetConsoleOutputCP', exception: false)
return successful_or_if_not_messageout (r, 'SetConsoleOutputCP', exception: false)
end

def get_console_codepage()
Expand All @@ -460,8 +460,7 @@ def get_console_output_codepage()

def generate_console_ctrl_event(event, pgrp)
r = GenerateConsoleCtrlEvent(event, pgrp)
error_message(r, 'GenerateConsoleCtrlEvent')
return r != 0
return successful_or_if_not_messageout (r, 'GenerateConsoleCtrlEvent')
end

# Ctrl+C trap support
Expand Down

0 comments on commit 24339e6

Please sign in to comment.