Module: FFI

Extended by:
Library
Defined in:
lib/ffi/win32/extensions.rb

Class Method Summary collapse

Class Method Details

.raise_windows_error(function, err = FFI.errno) ⇒ Object

Raises a Windows specific error using SystemCallError that is based on the err provided, with the message prepended with the function name.

Raises:

  • (SystemCallError)


100
101
102
# File 'lib/ffi/win32/extensions.rb', line 100

def raise_windows_error(function, err = FFI.errno)
  raise SystemCallError.new(windows_error_message(function, err), err)
end

.windows_error_message(function, err = FFI.errno) ⇒ Object

Returns a Windows specific error message based on err prepended with the function name. Note that this does not actually raise an error, it only returns the message.

The message will always be English regardless of your locale.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ffi/win32/extensions.rb', line 80

def windows_error_message(function, err = FFI.errno)
  error_message = ""

  # ARGUMENT_ARRAY + SYSTEM + MAX_WIDTH_MASK
  flags = 0x00001000 | 0x00000200 | 0x000000FF

  # 0x0409 = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)
  # We use English for errors because Ruby uses English for errors.

  FFI::MemoryPointer.new(:char, 1024) do |buf|
    length = FormatMessage(flags, nil, err , 0x0409, buf, buf.size, nil)
    error_message = function + ": " + buf.read_string(length).strip
  end

  error_message
end