By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

Why can't I perform USB File Stream Read/Write on Windows 7

P: n/a
Im facing a problem with file stream read/write for a usb device. The code works fine on XP (32 bit). but fails to work on Windows 7 (64 bit).
I'm successfully past problem of getting the device to enumarate and getting the file handle
However the fileStream Read/Write doesn't work

I used the managed file stream method i.e first create a handle via native call in overlapped mode. Then use the handle to get a FileStream object and perform read/write using it. On XP 32 bit it works great but on Windows 7 the FileStream.Write throws a null reference exception.

Then on Windows 7 the approach I took was read/write using native calls (I abandoned the managed FileStream class approach). For this I used WriteFile, ReadFile, GetOverlappedResult APIs with OVERLAPPED structure etc. I have also pinned the data buffers. But ReadFile (or WriteFile) returns 997 i.e ERROR_IO_PENDING and subsequent call to GetOverlappedResult returns 1 ie. ERROR_INVALID_FUNCTION. Then NumberOfBytesRead is always 0.

Im not sure what am I doing wrong!!!.

Following is a code snippet containing the read API.

Thanks In advance for all the help.

Expand|Select|Wrap|Line Numbers
  1.         public byte[] Read()
  2.         {
  3.             OVERLAPPED ovlpd = new OVERLAPPED();
  4.             byte [] buffer = new byte[512];
  5.             int ErrorCode = 0;
  6.             uint NumberOfBytesRead = 0;
  7.             const uint ERROR_IO_PENDING = 997;
  8.  
  9.             GCHandle PinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);    
  10.             ovlpd.Offset = 0;
  11.             ovlpd.OffsetHigh = 0;
  12.             ovlpd.EventHandle = ewhRead.SafeWaitHandle.DangerousGetHandle();
  13.             ovlpd.Internal = UIntPtr.Zero;
  14.             ovlpd.InternalHigh = UIntPtr.Zero;
  15.             GCHandle PinnedOvlpd = GCHandle.Alloc(ovlpd, GCHandleType.Pinned);
  16.  
  17.             // ewhRead is declared and instantiate outside
  18.             // EventWaitHandle ewhRead = new EventWaitHandle(false, EventResetMode.ManualReset);
  19.             ewhRead.Reset();
  20.  
  21.             if(ReadFile(m_hDevFsHandle,
  22.                 PinnedBuffer.AddrOfPinnedObject(),
  23.                 (uint) 512,
  24.                 out NumberOfBytesRead,
  25.                 PinnedOvlpd.AddrOfPinnedObject()))
  26.             {
  27.                 ErrorCode = Marshal.GetLastWin32Error();
  28.                 Dbg.WriteLine("Read Success: {0}", ErrorCode);
  29.             }
  30.             else
  31.             {
  32.                 ErrorCode = Marshal.GetLastWin32Error();
  33.                 Dbg.WriteLine("Read Error: {0}", ErrorCode);
  34.  
  35.                 if (ErrorCode == ERROR_IO_PENDING)
  36.                 {
  37.                     if (!ewhRead.WaitOne(5000))
  38.                     {
  39.                         Dbg.WriteLine("Wait Timed out");
  40.                     }
  41.  
  42.                     if (0 != GetOverlappedResult(m_hDevFsHandle,
  43.                         PinnedOvlpd.AddrOfPinnedObject(),
  44.                         out NumberOfBytesRead,
  45.                         1))
  46.                     {
  47.                         ErrorCode = Marshal.GetLastWin32Error();
  48.                         Dbg.WriteLine("GetOverlappedResult Success: {0}", ErrorCode);
  49.                     }
  50.                     else
  51.                     {
  52.                         ErrorCode = Marshal.GetLastWin32Error();
  53.                         Dbg.WriteLine("GetOverlappedResult Error: {0}", ErrorCode);
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     CancelIo(m_hDevFsHandle);
  59.                 }
  60.  
  61.                 Dbg.WriteLine("Read {0}", NumberOfBytesRead);
  62.             }
  63.  
  64.  
  65.             PinnedBuffer.Free();
  66.             PinnedOvlpd.Free();
  67.  
  68.             return buffer;
  69.         }
  70.  
  71.  
Nov 2 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.