[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Raw Device Access in win32

Tim Hoolihan

5/31/2006 3:26:00 PM

I've written a program in ruby that runs great on linux, reading an
optical drive with code like the following:

File.open("/dev/hdc")

When trying to port this to allow a friend to run it on win32, I can not
figure out how to get raw access to the device.

I have tried:

File.open("E")
File.open("E:")
File.open("\\.\E:")
File.open("\\\\.\\E:") #produces \\.\E: which is mentioned in
#http://support.microsoft.com/k...

None of which work. Any ideas?
Thanks!
5 Answers

Chris Hulan

5/31/2006 5:44:00 PM

0

I played with File, Dir and Pathname but all seem to assume the usual
access.

For Raw access I think you'll need to investgate using win32API and the
Win32 CreateFile function (and any others you may need) from
kernel32.dll

Cheers
Chris

Digikata@gmail.com

5/31/2006 8:51:00 PM

0

Tim Hoolihan wrote:
> I've written a program in ruby that runs great on linux, reading an
> optical drive with code like the following:
>
> File.open("/dev/hdc")
>
> When trying to port this to allow a friend to run it on win32, I can not
> figure out how to get raw access to the device.
>
> I have tried:
>
> File.open("E")
> File.open("E:")
> File.open("\\.\E:")
> File.open("\\\\.\\E:") #produces \\.\E: which is mentioned in
> #http://support.microsoft.com/k...
>
> None of which work. Any ideas?
> Thanks!

The support link you supplied says to use \\,\PhysicalDriveN (where N
is an integer). I don't see that in your list of tries - have you tried
"\\,\PhysicalDrive3" for example? I guess the file string would be
"\\\\,\\PhysicalDrive3" ... don't know which drive is right on your
system.
- alan

Daniel Berger

6/1/2006 12:34:00 AM

0

Tim Hoolihan wrote:
> I've written a program in ruby that runs great on linux, reading an
> optical drive with code like the following:
>
> File.open("/dev/hdc")
>
> When trying to port this to allow a friend to run it on win32, I can not
> figure out how to get raw access to the device.
>
> I have tried:
>
> File.open("E")
> File.open("E:")
> File.open("\\.\E:")
> File.open("\\\\.\\E:") #produces \\.\E: which is mentioned in
> #http://support.microsoft.com/k...
>
> None of which work. Any ideas?
> Thanks!

require "windows/file"
require "windows/error"
require "windows/handle"

class File
include Windows::File
include Windows::Error
include Windows::Handle
extend Windows::File
extend Windows::Error
extend Windows::Handle

def self.opendir(dir)
dirhandle = CreateFile(
dir,
GENERIC_READ,
FILE_SHARE_READ, #|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
0,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0
)

if dirhandle == INVALID_HANDLE_VALUE
raise StandardError, get_last_error
end

begin
yield dirhandle
ensure
CloseHandle(dirhandle)
end
end
end

# example
File.opendir("C:\\"){ |dh| ... }

Be careful....

Regards,

Dan

Tim Hoolihan

6/1/2006 6:59:00 PM

0

Thanks for the post. I'm not having any luck so far. I added a few
methods to the class (Which I renamed WFile).

def WFile.read(handle,bytes)
buf=""
bytes_read=0
ReadFile(handle, buf, bytes, bytes_read,nil)
return buf
end
def WFile.size(handle)
size=0
GetFileSize(handle,size)
return size
end

Then the meat of the program is:

def rip(source,dest)
WFile.opendir(source) { |src|
size = WFile.size(src)
progress=0
File.open(dest,"wb") { |fout|
while buff=WFile.read(src,1024)
fout.write buff
progress+=buff.length
yield(progress,size)
end
}
}
end

def wrip(source,dest)

end

RDoc::usage() unless ARGV[1]
mb=1024*1024
d = ARGV[0].gsub(':','')
d.gsub!('\\','')
puts "attempting to rip drive #{d} to #{ARGV[1]}"
drive = "\\\\.\\#{d}:"
wrip(drive, ARGV[1]) { |prog,total|
puts "copied #{prog/mb} of #{total/mb} MB"
}

As you can see, it basically just makes an image of drive. I may not be
calling the api function right. I have not tried the PhysicalDriveN
method (that another poster mentioned) as the open is returning a valid
file handle (indicating success). It's my call to the read that is
causing problems.

Anyway, I wanted to post the progress so far, but only play around with
this if you want to. I'm not going to put too much more time into it
considering that I'm creating a program to solve a problem that is
already solved. I was hoping I could easily port the program, but this
is quickly becoming a total rewrite.

Thanks,
-Tim

Daniel Berger wrote:
> Tim Hoolihan wrote:
>> I've written a program in ruby that runs great on linux, reading an
>> optical drive with code like the following:
>>
>> File.open("/dev/hdc")
>>
>> When trying to port this to allow a friend to run it on win32, I can not
>> figure out how to get raw access to the device.
>>
>> I have tried:
>>
>> File.open("E")
>> File.open("E:")
>> File.open("\\.\E:")
>> File.open("\\\\.\\E:") #produces \\.\E: which is mentioned in
>> #http://support.microsoft.com/k...
>>
>> None of which work. Any ideas?
>> Thanks!
>
> require "windows/file"
> require "windows/error"
> require "windows/handle"
>
> class File
> include Windows::File
> include Windows::Error
> include Windows::Handle
> extend Windows::File
> extend Windows::Error
> extend Windows::Handle
>
> def self.opendir(dir)
> dirhandle = CreateFile(
> dir,
> GENERIC_READ,
> FILE_SHARE_READ, #|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
> 0,
> OPEN_EXISTING,
> FILE_FLAG_BACKUP_SEMANTICS,
> 0
> )
>
> if dirhandle == INVALID_HANDLE_VALUE
> raise StandardError, get_last_error
> end
>
> begin
> yield dirhandle
> ensure
> CloseHandle(dirhandle)
> end
> end
> end
>
> # example
> File.opendir("C:\\"){ |dh| ... }
>
> Be careful....
>
> Regards,
>
> Dan
>

Tim Hoolihan

6/1/2006 7:00:00 PM

0

correction:
obviously that's supposed to be a call to rip not wrip, i just posted
with what was left of some debug code in...

Tim Hoolihan wrote:
> Thanks for the post. I'm not having any luck so far. I added a few
> methods to the class (Which I renamed WFile).
>
> def WFile.read(handle,bytes)
> buf=""
> bytes_read=0
> ReadFile(handle, buf, bytes, bytes_read,nil)
> return buf
> end
> def WFile.size(handle)
> size=0
> GetFileSize(handle,size)
> return size
> end
>
> Then the meat of the program is:
>
> def rip(source,dest)
> WFile.opendir(source) { |src|
> size = WFile.size(src)
> progress=0
> File.open(dest,"wb") { |fout|
> while buff=WFile.read(src,1024)
> fout.write buff
> progress+=buff.length
> yield(progress,size)
> end
> }
> }
> end
>
> def wrip(source,dest)
>
> end
>
> RDoc::usage() unless ARGV[1]
> mb=1024*1024
> d = ARGV[0].gsub(':','')
> d.gsub!('\\','')
> puts "attempting to rip drive #{d} to #{ARGV[1]}"
> drive = "\\\\.\\#{d}:"
> wrip(drive, ARGV[1]) { |prog,total|
> puts "copied #{prog/mb} of #{total/mb} MB"
> }
>
> As you can see, it basically just makes an image of drive. I may not be
> calling the api function right. I have not tried the PhysicalDriveN
> method (that another poster mentioned) as the open is returning a valid
> file handle (indicating success). It's my call to the read that is
> causing problems.
>
> Anyway, I wanted to post the progress so far, but only play around with
> this if you want to. I'm not going to put too much more time into it
> considering that I'm creating a program to solve a problem that is
> already solved. I was hoping I could easily port the program, but this
> is quickly becoming a total rewrite.
>
> Thanks,
> -Tim
>
> Daniel Berger wrote:
>> Tim Hoolihan wrote:
>>> I've written a program in ruby that runs great on linux, reading an
>>> optical drive with code like the following:
>>>
>>> File.open("/dev/hdc")
>>>
>>> When trying to port this to allow a friend to run it on win32, I can not
>>> figure out how to get raw access to the device.
>>>
>>> I have tried:
>>>
>>> File.open("E")
>>> File.open("E:")
>>> File.open("\\.\E:")
>>> File.open("\\\\.\\E:") #produces \\.\E: which is mentioned in
>>> #http://support.microsoft.com/k...
>>>
>>> None of which work. Any ideas?
>>> Thanks!
>> require "windows/file"
>> require "windows/error"
>> require "windows/handle"
>>
>> class File
>> include Windows::File
>> include Windows::Error
>> include Windows::Handle
>> extend Windows::File
>> extend Windows::Error
>> extend Windows::Handle
>>
>> def self.opendir(dir)
>> dirhandle = CreateFile(
>> dir,
>> GENERIC_READ,
>> FILE_SHARE_READ, #|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
>> 0,
>> OPEN_EXISTING,
>> FILE_FLAG_BACKUP_SEMANTICS,
>> 0
>> )
>>
>> if dirhandle == INVALID_HANDLE_VALUE
>> raise StandardError, get_last_error
>> end
>>
>> begin
>> yield dirhandle
>> ensure
>> CloseHandle(dirhandle)
>> end
>> end
>> end
>>
>> # example
>> File.opendir("C:\\"){ |dh| ... }
>>
>> Be careful....
>>
>> Regards,
>>
>> Dan
>>