函数:
BOOL ReadFile(
  HANDLE hFile,                // handle to file
  LPVOID lpBuffer,             // data buffer
  DWORD nNumberOfBytesToRead,  // number of bytes to read
  LPDWORD lpNumberOfBytesRead, // number of bytes read
  LPOVERLAPPED lpOverlapped    // overlapped buffer
);
中的最后一个参数中的hEvent应该赋什么值?为什么像下面这样用不行?
OVERLAPPED overlapped;
ReadFile(....,&overlapped);

解决方案 »

  1.   

    lpOverlapped 
    Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED. 
    If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was created with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the read operation is complete. If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure and ReadFile may return before the read operation has been completed. In this case, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue while the read operation finishes. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the read operation. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed. If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed. 
      

  2.   

    OVERLAPPED
    The OVERLAPPED structure contains information used in asynchronous input and output (I/O). typedef struct _OVERLAPPED { // o 
        DWORD  Internal; 
        DWORD  InternalHigh; 
        DWORD  Offset; 
        DWORD  OffsetHigh; 
        HANDLE hEvent; 
    } OVERLAPPED; 
     
    Members
    Internal 
    Reserved for operating system use. This member, which specifies a system-dependent status, is valid when the GetOverlappedResult function returns without setting the extended error information to ERROR_IO_PENDING. 
    InternalHigh 
    Reserved for operating system use. This member, which specifies the length of the data transferred, is valid when the GetOverlappedResult function returns TRUE. 
    Offset 
    Specifies a file position at which to start the transfer. The file position is a byte offset from the start of the file. The calling process sets this member before calling the ReadFile or WriteFile function. This member is ignored when reading from or writing to named pipes and communications devices. 
    OffsetHigh 
    Specifies the high word of the byte offset at which to start the transfer. This member is ignored when reading from or writing to named pipes and communications devices. 
    hEvent 
    Handle to an event set to the signaled state when the transfer has been completed. The calling process sets this member before calling the ReadFile, WriteFile, ConnectNamedPipe, or TransactNamedPipe function. 
    Res
    You can use the HasOverlappedIoCompleted macro to determine whether an asynchronous I/O operation has completed. You can use the CancelIo function to cancel an asynchronous I/O operation. QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in winbase.h.See Also
    Synchronization Overview, Synchronization Structures, CancelIo, ConnectNamedPipe, CreateFile, GetOverlappedResult, HasOverlappedIoCompleted, ReadFile, ReadFileEx, TransactNamedPipe, WriteFile, WriteFileEx  
      

  3.   

    试试把
    OVERLAPPED overlapped;
    ReadFile(....,&overlapped);
    更改成
    OVERLAPPED overlapped;
    ZoeroMemory(&overlapped,sizeof(overlapped));
    ReadFile(....,&overlapped);
      

  4.   

    int CComm::ReadCommBlock( LPVOID lpszBuf, DWORD nMax )
    {
    COMSTAT ComStat;
    DWORD dwCommError, dwLen;
    OVERLAPPED os; if( !m_bCommOpen )
    return FALSE; ClearCommError( m_hComm, &dwCommError, &ComStat );
    dwLen = ComStat.cbInQue < nMax ? ComStat.cbInQue : nMax; memset( &os, 0, sizeof( os ) );
    os.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); if( dwLen > 0 && !ReadFile( m_hComm, lpszBuf, dwLen, &dwLen, &os ) )
    {
    if( GetLastError() == ERROR_IO_PENDING )
    {
    while( !GetOverlappedResult( m_hComm, &os, &dwLen, TRUE ) )
    {
    if( GetLastError() == ERROR_IO_INCOMPLETE )
    continue;
    else
    {
    ShowError();
    break;
    }
    }
    }
    else
    {
    ShowError();
    }
    } CloseHandle(os.hEvent);
    return dwLen;
    }
      

  5.   

    qing_li73(bluemoon),你贴的东西都是MSDN里面的。
    我想回贴子,最好是说自己想说的话。as2001(),你的方法我试过了,不行。
      

  6.   

    lpNumberOfBytesRead參數設置為NULL。
      

  7.   

    关键时你定义了OVERLAPPED,但是没有为他分配到合适的空间,所以使用
    ReadFile(....,&overlapped);
    调用就会出错。根该如下:OVERLAPPED overlapped;
    memset(&overlapped,0,sizeof(overlapped));
    overlapped.hEvent = CreateEvent( 0,true,0,0);
    ReadFile(....,&overlapped);