473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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

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
0 1804

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

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
4
by: Feng Chun | last post by:
Hi, need help for this. In asp.net, when I do a load balancing on my website, which means there are 2 or more servers behind, one of my webpage needs to read/write a temp file in the server. But...
3
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the...
1
by: mulham.haffar | last post by:
hi guys.. im writing an application that uses windows service to listen (as a tcplistener) for any data sent (by a tcpclient) ... one kind of the requests might be a file sent by client and the...
7
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
3
by: markclinn | last post by:
I have a device in the field that I access by the stream method. I open the Stream and do the following: 1. stream.write a character to the device. 2. stream.read the information from the...
4
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hi, I have read the "how to's" on MSDN including this one, http://msdn2.microsoft.com/en-us/library/36b93480.aspx, on reading/writing files in C#, but haven't found something that I can put...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
In C I can open a file as readable, writable, or both. Then I can arbitrarily do either operation without closing the file between operations as long as I perform some kind of file position...
1
Xx r3negade
by: Xx r3negade | last post by:
Sorry for the extreme newbie question, but I honestly can't find this anywhere else. When I do f = open('/var/www/some_file', 'r+') it is supposed to open the file for reading and writing,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.