4

I'm having some problems writing to a FileStream writing to a SafeFileHandle, this file is used to write data to a HID device. I'll post snippets of the code since these occur in several different objects.

This is the handle creation code:

HidHandle = FileIO.CreateFile(pDevicePathName, FileIO.GENERIC_READ | FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0);

The handle returned is valid.

Then the stream:

FileStreamDevice = new FileStream(HidHandle, FileAccess.ReadWrite, 65, true);

The stream is created succesfully, but both Position and Length return NotSupportedException (which afaik, is normal).

Then I send the message:

byte[] pMsg = new byte[65];    

ManualResetEvent manualevent = new ManualResetEvent(false);

IAsyncResult asynResult = device.FileStreamDevice.BeginWrite(pMsg, 0, pMsg.Length, 
            new AsyncCallback(End_Write), new DeviceAsyncState(device.FileStreamDeviceData, manualevent));

This immediately returns the following exception message:

 'The parameter is incorrect'

This is the top of the stack trace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.BeginWriteCore(Byte[] bytes, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object stateObject) 

Thanks in advance.

  • You are setting the *isAsync* argument of the constructor to true. But that will only work if the *HidHandle* was opened for overlapped I/O. If it wasn't then it will fail with the statement exception message. You'll need to use Write() instead of BeginWrite(). – Hans Passant Jul 16 '13 at 22:11
  • The HidHandle was opened for overlapped I/O, it has the "FileIO.FILE_FLAG_OVERLAPPED" parameter set. – Ezequiel Lewin Jul 17 '13 at 15:28
  • What happens when you call Write() instead of BeginWrite()? If it still fails then do consider that this error code comes from the driver. – Hans Passant Jul 17 '13 at 15:32
  • Did you resolve this issue? I'm having similar problems with writing to a HID – Sverrir Sigmundarson Nov 20 '13 at 23:58
  • Yes, the problem was that the HID device required me to write the exact number of bytes as it's output buffer size. – Ezequiel Lewin Nov 21 '13 at 14:55
  • Thanks was having the same problem because I was picking the max size of the input / output report lengths for the stream but when the input length is larger than the output length it will create an error. Problem is now I gotta figure out how to receive all the data in the smaller buffer – Fatlad Feb 26 '14 at 19:49

1 Answers1

0

did you check the given win errorcode in the in the exception? concerning the trace there should be an error code contained.

are there limitstions of the datalenth you are perhaps exceeding?