FileStream and asynchronous I / O with the device

I am having trouble writing a FileStream file to SafeFileHandle, this file is used to write data to the HID device. I will post code snippets as they are found in several different objects.

This is the descriptor 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 returned handle is valid.

Then the stream:

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

The thread is created successfully, but both Position and Length return a NotSupportedException (which is afaik, this is normal).

Then I send a 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.

+4
source share
1 answer

Have you checked this win error code in the exception? the trace should contain an error code.

Are there any datalenth length limits that you possibly exceed?

0
source

Source: https://habr.com/ru/post/1491720/


All Articles