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

Heesob Park phasis at gmail.com
Mon Oct 21 05:05:23 UTC 2013


Hi,

2013/10/21 Daniel Berger <djberg96 at gmail.com>:
> 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?
>
The INVALID_HANDLE_VALUE  is not 0xFFFFFFFF but 0xFFFFFFFFFFFFFFFF on
64-bit Ruby.
>
> # 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
>
INVALID_HANDLE_VALUE = 0xFFFFFFFF
should be
INVALID_HANDLE_VALUE = FFI::Pointer.new(-1).address

Regards,
Park Heesob


More information about the win32utils-devel mailing list