Open a windows device file handle in ruby?

Q

Qaz Qaz

Hello, I have windows device driver which creates a symbolic link for
the path '\\.\MyDevice'. In C I can then use CreateFile() to open this
device, e.g.

---[c
snippet]------------------------------------------------------------------
handle = CreateFile( "\\\\.\\MyDevice", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );

WriteFile( handle, data, len, &written, NULL );

CloseHandle( handle );
---[c
snippet]------------------------------------------------------------------

However in Ruby, I should be able to simple use something like:

---[ruby
snippet]---------------------------------------------------------------
handle = open( "\\\\.\\MyDevice", File::RDWR )
handle.syswrite( data )
handle.close
---[ruby
snippet]---------------------------------------------------------------

but it does not work and segfaults the ruby interpreter during the open!

Can anybody please help as to what I am doing wrong. How do you open a
file handle to a device in windows?

Thanks!
 
C

Charles Calvert

Hello, I have windows device driver which creates a symbolic link for
the path '\\.\MyDevice'. In C I can then use CreateFile() to open this
device, e.g.

---[c
snippet]------------------------------------------------------------------
handle = CreateFile( "\\\\.\\MyDevice", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );

WriteFile( handle, data, len, &written, NULL );

CloseHandle( handle );
---[c
snippet]------------------------------------------------------------------

However in Ruby, I should be able to simple use something like:

---[ruby
snippet]---------------------------------------------------------------
handle = open( "\\\\.\\MyDevice", File::RDWR )
handle.syswrite( data )
handle.close
---[ruby
snippet]---------------------------------------------------------------

but it does not work and segfaults the ruby interpreter during the open!

Your assumption that you should be able to use standard Ruby IO
functions seems suspicious to me. What happens if you write C code
that attempts to open the file using fopen() and write to it with
fputs()? If that fails, then you have your answer.

Instead, you could call the Win32API functions CreateFile() and
WriteFile() from Ruby using either the Ruby Win32API library or
dl/win32 (see
http://stdlib.rubyonrails.org/libdoc/dl/rdoc/classes/Win32API.html#M000496).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top