--- q_a/samples/event/asyn_io.c 2018/08/09 18:29:40 1.1.1.2 +++ q_a/samples/event/asyn_io.c 2018/08/09 18:30:08 1.1.1.3 @@ -1,8 +1,15 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + /****************************************************************************\ -** ** -** Microsoft Developer Support ** -** Copyright (c) 1992 Microsoft Corporation ** -** ** ** MODULE: IO.C ** ** ** ** ** @@ -68,30 +75,30 @@ void main ( int argc, char *argv[] ) HEVENT hEvent ; // Handle to the synchronization Event OVERLAPPED - OverLapped ; // The operating system does not automatically - // track the current file position of a file - // open for asynchroous I/O. Instead one must - // manually track this by updating the OVERLAPPED - // structure. To update this structure one uses - // the result returned by GetOverLappedResults(). + OverLapped ; // The operating system does not automatically + // track the current file position of a file + // open for asynchroous I/O. Instead one must + // manually track this by updating the OVERLAPPED + // structure. To update this structure one uses + // the result returned by GetOverLappedResults(). - CHAR buf[SECTOR_SIZE] ; // Buffer used by either - // ReadFile() or WriteFile() + CHAR buf[SECTOR_SIZE] ; // Buffer used by either + // ReadFile() or WriteFile() - DWORD cbBytesRead, // count of bytes read by ReadFile() - // When asynchonous I/O is performed - // one must check how many bytes were - // read by calling GetOverLappedResults(). + DWORD cbBytesRead, // count of bytes read by ReadFile() + // When asynchonous I/O is performed + // one must check how many bytes were + // read by calling GetOverLappedResults(). - cbBytesWritten, // count of bytes written by WriteFile() + cbBytesWritten, // count of bytes written by WriteFile() - cbBytesTransfered ; // this value is returned by - // GetOverLappedResults() represents the - // count of bytes transfered asynchronous + cbBytesTransfered ; // this value is returned by + // GetOverLappedResults() represents the + // count of bytes transfered asynchronous - DWORD dwStatus, // value returned by GetLastError() + DWORD dwStatus, // value returned by GetLastError() dwSuccess ; // return code of last API called BOOL bSuccess ; // return code of last API called @@ -99,12 +106,12 @@ void main ( int argc, char *argv[] ) CHAR buffer[80] ; /***************************************************************************\ -** ** +** ** ** This block of code initalizes all the necessary variables needed to do ** ** asynchronous IO. Specfically it opens the files and initializes the ** ** synchronization EVENT and OVERLAPPED structure needed to keep track ** ** of asynchronous file I/O ** -** ** +** ** \***************************************************************************/ @@ -122,15 +129,15 @@ void main ( int argc, char *argv[] ) */ hInfile = CreateFile - ( - argv[1] // File to read - ,GENERIC_READ // Open file for reading only - ,FILE_SHARE_READ // Allow others to read file - ,NULL // No security - ,OPEN_EXISTING // Fail if file doesn't exit - ,FILE_FLAG_OVERLAPPED // Async I/o - ,DEFAULT - ) ; + ( + argv[1] // File to read + ,GENERIC_READ // Open file for reading only + ,FILE_SHARE_READ // Allow others to read file + ,NULL // No security + ,OPEN_EXISTING // Fail if file doesn't exit + ,FILE_FLAG_OVERLAPPED // Async I/o + ,DEFAULT + ) ; if ( hInfile == IO_ERROR ) @@ -146,15 +153,15 @@ void main ( int argc, char *argv[] ) */ hOutfile = CreateFile - ( - argv[2] // File to write to - ,GENERIC_WRITE // Open file only to write to - ,DEFAULT // No sharing allowed - ,NULL // No security - ,CREATE_NEW // Fail if file exists - ,FILE_ATTRIBUTE_NORMAL // No attributes - ,DEFAULT - ) ; + ( + argv[2] // File to write to + ,GENERIC_WRITE // Open file only to write to + ,DEFAULT // No sharing allowed + ,NULL // No security + ,CREATE_NEW // Fail if file exists + ,FILE_ATTRIBUTE_NORMAL // No attributes + ,DEFAULT + ) ; if ( hOutfile == IO_ERROR ) @@ -171,12 +178,12 @@ void main ( int argc, char *argv[] ) */ hEvent = CreateEvent - ( - NULL // No security - ,TRUE // Manual reset - ,FALSE // Initially Event set to non-signaled state - ,NULL // No name - ) ; + ( + NULL // No security + ,TRUE // Manual reset + ,FALSE // Initially Event set to non-signaled state + ,NULL // No name + ) ; if ( !hEvent ) ErrorMsg ( "Error creating Event" ) ; @@ -201,7 +208,7 @@ void main ( int argc, char *argv[] ) ** This is the main function of this program. Here we loop until the file ** ** has been copied. Notice that even though we are using asynchronous I/O ** ** ReadFile() may still return sychronously if it can be done fast enough ** -** ** +** ** \***************************************************************************/ while ( bSuccess ) @@ -212,14 +219,14 @@ void main ( int argc, char *argv[] ) */ bSuccess = ReadFile - ( - hInfile // Handle of file to read - ,buf // Buffer to store input - ,SECTOR_SIZE // Number of bytes to read - ,&cbBytesRead // Number of bytes read. See note] - // above in delcaration - ,&OverLapped // Used for asynchronous I/O - ) ; + ( + hInfile // Handle of file to read + ,buf // Buffer to store input + ,SECTOR_SIZE // Number of bytes to read + ,&cbBytesRead // Number of bytes read. See note] + // above in delcaration + ,&OverLapped // Used for asynchronous I/O + ) ; @@ -228,7 +235,7 @@ void main ( int argc, char *argv[] ) */ if ( !bSuccess && (( dwStatus = GetLastError ()) == ERROR_IO_PENDING ) ) { - printf ( "\nIO Pending" ) ; + printf ( "\nIO Pending" ) ; /* * Read not complete yet first execute the background task then check * to the if event "hEvent" set to the signaled state. If the event is @@ -241,9 +248,9 @@ void main ( int argc, char *argv[] ) dwSuccess = WaitForSingleObject ( hEvent, RETURN_IMEDIATELY ) ; - if ( dwSuccess == WAIT_ERROR ) - ErrorMsg("Error in WaitForSingleObject") ; - else if ( dwSuccess == SUCCESS ) + if ( dwSuccess == WAIT_ERROR ) + ErrorMsg("Error in WaitForSingleObject") ; + else if ( dwSuccess == SUCCESS ) { bSuccess = GetOverlappedResult( @@ -252,11 +259,11 @@ void main ( int argc, char *argv[] ) ErrorMsg ( "Error in GetOverLappedResult" ) ; else { - // Read has completed now find out how many bytes have been read + // Read has completed now find out how many bytes have been read - printf ( "\nNumber of bytes transfered is %d\n", cbBytesTransfered ) ; - OverLapped.Offset += cbBytesTransfered ; - cbBytesRead = cbBytesTransfered ; + printf ( "\nNumber of bytes transfered is %d\n", cbBytesTransfered ) ; + OverLapped.Offset += cbBytesTransfered ; + cbBytesRead = cbBytesTransfered ; } } else if ( dwSuccess == WAIT_TIMEOUT ) @@ -267,22 +274,22 @@ void main ( int argc, char *argv[] ) */ else if ( bSuccess && cbBytesRead != 0 ) { - printf ( "\nNumber of bytes transfered is %d\n", cbBytesRead ) ; - OverLapped.Offset += cbBytesRead ; + printf ( "\nNumber of bytes transfered is %d\n", cbBytesRead ) ; + OverLapped.Offset += cbBytesRead ; } /* * ReadFile() has found the end of the file; exit loop */ else if ( bSuccess && cbBytesRead == 0 ) { - printf ( "End of file read\n" ) ; - break ; + printf ( "End of file read\n" ) ; + break ; } /* * An error occured while reading, print out status and exit loop. */ else - ErrorMsg ( "Error reading to file" ) ; + ErrorMsg ( "Error reading to file" ) ; @@ -290,25 +297,25 @@ void main ( int argc, char *argv[] ) * Write to file synchronously */ bSuccess = WriteFile - ( - hOutfile // Handle of file to write to - ,buf // Data to write to file - ,cbBytesRead // Number of bytes to write - ,&cbBytesWritten // Number of bytes written - ,NULL // Only need for asynchonous output - ) ; + ( + hOutfile // Handle of file to write to + ,buf // Data to write to file + ,cbBytesRead // Number of bytes to write + ,&cbBytesWritten // Number of bytes written + ,NULL // Only need for asynchonous output + ) ; if ( !bSuccess ) - ErrorMsg ( "Error writing to file" ) ; + ErrorMsg ( "Error writing to file" ) ; } // End of WHILE loop - CloseHandle ( hInfile ) ; // Close handle to input file + CloseHandle ( hInfile ) ; // Close handle to input file FlushFileBuffers ( hOutfile ) ; // Make sure all data written to file first - CloseHandle ( hOutfile ) ; // Close handle to output file + CloseHandle ( hOutfile ) ; // Close handle to output file exit ( EXIT_SUCCESS ) ; }