[Win32utils-devel] ReadFileEx segfault

Daniel Berger djberg96 at gmail.com
Sat Oct 25 20:47:40 EDT 2008


Hi,

Windows Vista
Ruby 1.8.6-p114 - One click

I tried the following script below but it segfaults. One thing I realized is 
that we don't explicitly handle VOID callbacks (e.g. FileIOCompletionRoutine) in 
api.c. Could that be the issue?

require 'windows/file'
require 'windows/handle'
require 'windows/error'
require 'windows/synchronize'
require 'windows/thread'

include Windows::File
include Windows::Handle
include Windows::Error
include Windows::Synchronize
include Windows::Thread

file = "test.txt"
File.open(file, 'w'){ |fh| fh.puts "The quick brown fox jumped over" }

overlapped = 0.chr * 20
overlapped[8,4] = [4].pack("L")

handle = CreateFile(
    file,
    FILE_READ_DATA,
    FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    0
)

if handle == INVALID_HANDLE_VALUE
    raise get_last_error
end

buf = 0.chr * File.size(file)
bytes = [0].pack('L')

func = Win32::API::Callback.new('LLP', 'V'){ |err, bytes, olap|
    puts "Hello"
}

#bool = ReadFile(handle, buf, 5, bytes, overlapped)
bool = ReadFileEx(handle, buf, 5, overlapped, func)

unless bool
    error = GetLastError()
    if error == ERROR_IO_PENDING
       unless GetOverlappedResult(handle, overlapped, bytes, true)
          raise get_last_error
       end
    else
       CloseHandle(handle)
       raise error
    end
end

SleepEx(1,true)

CloseHandle(handle)

p buf.strip

Regards,

Dan


More information about the win32utils-devel mailing list