private unsafe IAsyncResult BeginWriteCore(byte[] array, int offset, int count, AsyncCallback userCallback, Object stateObject) { Debug.Assert(userCallback == null); Debug.Assert(stateObject == null); CheckParametersForBegin(array, offset, count); AsyncFileStream_AsyncResult asyncResult = new AsyncFileStream_AsyncResult(userCallback, stateObject, true); if (count == 0) { asyncResult.SignalCompleted(); } else { // Keep the array in one location in memory until the OS writes the // relevant data into the array. Free GCHandle later. asyncResult.PinBuffer(array); fixed(byte *p = array) { uint numBytesWritten = 0; bool res; byte[] byteArray = new byte[count]; Marshal.Copy(new IntPtr(p + offset), byteArray, 0, count); //res = Native.ReadFile(m_handle.DangerousGetHandle(), p + offset, count, out numBytesRead, asyncResult.OverlappedPtr); res = WinUsbDevice.WinUsb_WritePipe( m_winUsbDevice.DeviceInfo.winUsbHandle, System.Convert.ToByte(m_winUsbDevice.DeviceInfo.bulkOutPipe), byteArray, (uint)count, ref numBytesWritten, System.IntPtr.Zero); if (res == false) { if (HandleErrorSituation("BeginWrite", true)) { asyncResult.SignalCompleted(); } else { m_outstandingRequests.Add(asyncResult); } } else { asyncResult.m_numBytes = (int)numBytesWritten; asyncResult.SignalCompleted(); } } } return(asyncResult); }