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

Daniel Berger djberg96 at gmail.com
Mon Nov 4 03:30:30 UTC 2013


Ah, just realized that won't work on JRuby, which returns -1. Looks like
I'll have to check FFI::Platform::ADDRESS_SIZE.

Unless INVALID_HANDLE_VALUE actually is -1 when using JRuby. Is there an
easy way to test?

Regards,

Dan


On Sun, Oct 20, 2013 at 11:05 PM, Heesob Park <phasis at gmail.com> wrote:

> 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
> _______________________________________________
> win32utils-devel mailing list
> win32utils-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/win32utils-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/win32utils-devel/attachments/20131103/98da973a/attachment-0001.html>


More information about the win32utils-devel mailing list