Your browser does not allow scripts
Please click here to view a static table of contents without scripts
FT_W32_WriteFile
Return to Introduction  Previous page  Next page
Write data to the device.

BOOL
FT_W32_WriteFile
(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWritten, LPOVERLAPPED lpOverlapped)




Parameters
ftHandle
Handle of the device.
lpBuffer
Pointer to the buffer that contains the data to write to the device.
dwBytesToWrite
Number of bytes to be written to the device.
lpdwBytesWritten
Pointer to a variable that receives the number of bytes written to the device.
lpOverlapped
Pointer to an overlapped structure. Ignored in Linux.




Return Value
If the function is successful, the return value is nonzero.
If the function is unsuccessful, the return value is zero.



Remarks
This function supports both non-overlapped and overlapped I/O, except under Windows CE and Linux where only non-overlapped IO is supported.

Non-overlapped I/O
The parameter,
lpOverlapped, must be NULL for non-overlapped I/O.

This function always returns the number of bytes written in
lpdwBytesWritten.

This function does not return until
dwBytesToWrite have been written to the device.

When a write timeout has been setup in a previous call to
FT_W32_SetCommTimeouts, this function returns when the timer expires or dwBytesToWrite have been written, whichever occurs first. If a timeout occurred, lpdwBytesWritten contains the number of bytes actually written, and the function returns a non-zero value.

An application should always use the function return value and
lpdwBytesWritten. If the return value is non-zero and lpdwBytesWritten is equal to dwBytesToWrite then the function has completed normally. If the return value is non-zero and lpdwBytesWritten is less then dwBytesToWrite then a timeout has occurred, and the write request has been partially completed. Note that if a timeout occurred and no data was written, the return value is still non-zero.



Overlapped I/O

When the device has been opened for overlapped I/O, an application can issue a request and perform some additional work while the request is pending. This contrasts with the case of non-overlapped I/O in which the application issues a request and receives control again only after the request has been completed.

The parameter,
lpOverlapped, must point to an initialized OVERLAPPED structure.

This function completes immediately, and the return code is zero, signifying an error. An application should call
FT_W32_GetLastError to get the cause of the error. If the error code is ERROR_IO_PENDING, the overlapped operation is still in progress, and the application can perform other processing. Eventually, the application checks the result of the overlapped request by calling FT_W32_GetOverlappedResult.

If successful, the number of bytes written is returned in
lpdwBytesWritten.



Example

This example shows how to write 128 bytes to the device using non-overlapped I/O.

FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/o
char Buf[128]; // contains data to write to the device
DWORD dwToWrite = 128;
DWORD dwWritten;

if (FT_W32_WriteFile(ftHandle, Buf, dwToWrite, &dwWritten, &osWrite)) {
  if (dwToWrite == dwWritten){
    // FT_W32_WriteFile OK}
  else{
    // FT_W32_WriteFile timeout}
  }
else{
  // FT_W32_WriteFile failed}



This example shows how to write 128 bytes to the device using overlapped I/O.

FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/o
char Buf[128]; // contains data to write to the device
DWORD dwToWrite = 128;
DWORD dwWritten;
OVERLAPPED osWrite = { 0 };

if (!FT_W32_WriteFile(ftHandle, Buf, dwToWrite, &dwWritten, &osWrite)) {
  if (FT_W32_GetLastError(ftHandle) == ERROR_IO_PENDING) {
    // write is delayed so do some other stuff until ...
    if (!FT_W32_GetOverlappedResult(ftHandle, &osWrite, &dwWritten, FALSE)){
      // error}
    else {
      if (dwToWrite == dwWritten){
        // FT_W32_WriteFile OK}
      else{
        // FT_W32_WriteFile timeout}
    }
  }
}
else {
  // FT_W32_WriteFIle OK
}