[Win32utils-devel] CreateFile, FFI and 32 vs 64 bit Ruby

Daniel Berger djberg96 at gmail.com
Mon Oct 21 04:12:20 UTC 2013


Hi everyone,

I noticed that CreateFile seems to unexpectedly succeed when I run it with
a 64-bit Ruby. Did I declare the function wrong somehow?

Thanks,

Dan

# test.rb
require 'ffi'

class Windows
  extend FFI::Library
  ffi_lib :kernel32

  typedef :ulong, :dword
  typedef :uintptr_t, :handle
  typedef :pointer, :ptr
  typedef :string, :str

  attach_function :CreateFile, :CreateFileA, [:str, :dword, :dword, :ptr,
:dword, :dword, :handle], :handle
  attach_function :CloseHandle, [:handle], :bool

  INVALID_HANDLE_VALUE = 0xFFFFFFFF

  GENERIC_READ  = 0x80000000
  GENERIC_WRITE = 0x40000000
  FILE_SHARE_READ  = 1
  FILE_SHARE_WRITE = 2
  OPEN_EXISTING    = 3

  FILE_ATTRIBUTE_NORMAL = 0x00000080
  FILE_FLAG_WRITE_THROUGH = 0x80000000

  DEFAULT_OPEN_MODE = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH

  def self.open(name)
    handle = CreateFile(
      name,
      GENERIC_READ | GENERIC_WRITE,
      FILE_SHARE_READ | FILE_SHARE_WRITE,
      nil,
      OPEN_EXISTING,
      DEFAULT_OPEN_MODE,
      0
    )

    if handle == INVALID_HANDLE_VALUE
      raise SystemCallError.new('CreateFile', FFI.errno)
    end

    CloseHandle(handle) if handle
  end
end

if $0 == __FILE__
  Windows.open('bogus')
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/win32utils-devel/attachments/20131020/474df954/attachment-0001.html>


More information about the win32utils-devel mailing list