From: Alex Young Date: 2011-12-20T00:18:00+09:00 Subject: Re: Windows, CreateFile and IO objects Jeremy Bopp wrote in post #1037344: > On 12/19/2011 08:32 AM, Alex Young wrote: >> I'm porting some code from Linux to Windows which relies on direct >> access to a CD's block device. This "just works" on Linux if I >> File.open("/dev/cdrom"), but I can't see how to make it work on Windows. >> >> While manually running a dl/imported CreateFile works and gives me a >> valid handle, just running File.open("\\\\.\\D:", "r") crashes the >> interpreter (1.9.3-p0), as does IO.sysopen. >> >> Is there a built-in way to turn an open handle into a File object? I >> blindly tried IO.for_fd, but that fails and I got no further. > > I'm not expert on this, but my quick look in MSDN seems to indicate that > you need to use DeviceIoControl to handle reads and writes to the device > handle once you get it from CreateFile: Drat. I was afraid of that. > Regular files wouldn't be accessed using that function, so that's > probably why Ruby chokes on file objects you create with those special > device paths. > > If you want the object you may eventually create in Ruby to work mostly > like an IO object, take a look at my io-like gem: > > https://rubygems.org/gems/io-like > > Unfortunately, it doesn't support the Ruby 1.9 IO API yet, but it should > get you the basics without a ton of work. :-) > That looks interesting, I'll give it a shot. Do you know if this is likely to break horribly? class MyFunkyBlockDevice < IO include IO::Like end For some fairly boring reasons I do actually want an IO subclass, but I only need reading and seeking to work. Thanks, -- Alex -- Posted via http://www.ruby-forum.com/.