[Win32utils-devel] FFI and errno on Windows

Daniel Berger djberg96 at gmail.com
Wed Apr 11 16:16:48 UTC 2012


I'm a bit confused by error handling for posixy functions on Windows.
Consider the following code where I'm intentionally passing a bad
template to _mktemp. The docs say it should return EINVAL. My first
problem is not knowing exactly what the docs mean when they talk about
EINVAL on Windows. My second problem is that _get_errno returns a
different value that FFI.errno, and FFI.errno seems to return the same
value as GetLastError().

require 'ffi'
class Windows
  extend FFI::Library
  ffi_lib FFI::Library::LIBC

  attach_function :_mktemp, [:pointer], :string
  attach_function :_get_errno, [:pointer], :int

  ffi_lib :kernel32

  attach_function :GetLastError, [], :int

  def self.temp(template)
    result = _mktemp(template)

    err = get_err_num # 22
    #err = FFI.errno # 158
    #err = GetLastError() # 158

    if result.nil?
      raise SystemCallError, err, '_mktemp'
    end

    result
  end

  def self.get_err_num
    ptr = FFI::MemoryPointer.new(:int)

    if _get_errno(ptr) != 0
      raise SystemCallError, FFI.errno, '_get_errno'
    end

    ptr.read_int
  end
end

Windows.temp('xx')

Any insight on the subject of GetLastError vs _get_errno would be
greatly appreciated. My google skills seem to be failing.

Regards,

Dan


More information about the win32utils-devel mailing list