Open a windows device file handle in ruby?

Discussion in 'Ruby' started by Qaz Qaz, Oct 7, 2010.

  1. Qaz Qaz

    Qaz Qaz Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
    Qaz Qaz, Oct 7, 2010
    #1
    1. Advertising

  2. On Thu, 7 Oct 2010 11:43:29 -0500, Qaz Qaz <>
    wrote in <>:

    >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).

    --
    Charles Calvert
    Moderator - alt.computer.consultants.moderated
    Submission Address:
    Contact Address:
    Charles Calvert, Oct 20, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    627
    Joe Smith
    Jul 22, 2004
  2. nishadixit
    Replies:
    5
    Views:
    1,239
    Joe Discar
    Jun 1, 2005
  3. Replies:
    1
    Views:
    417
    santosh
    Nov 27, 2006
  4. Matt
    Replies:
    0
    Views:
    461
  5. manux

    Open cdrom device on windows

    manux, May 7, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    88
    Kevin Collins
    May 7, 2004
Loading...

Share This Page