lpOverlapped A pointer to an OVERLAPPED structure.This structure is required if hFile is created with FILE_FLAG_OVERLAPPED.If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure.If hFile is created with FILE_FLAG_OVERLAPPED and lpOverlappedis NULL, the function can report incorrectly that the read operation is complete.If hFile is opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure, and ReadFile may return before the read operation is complete. In this scenario, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING, which allows the calling process to continue while the read operation completes. The event specified in the OVERLAPPED structure is set to the signaled state when the read operation is complete, and then the caller must adjust the position of the file pointer.ReadFile resets the event specified by the hEvent member of the OVERLAPPED structure to a nonsignaled state when it begins the I/O operation. Therefore, the caller does not need to do that.If hFile is 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 is complete, and then the system updates the file pointer.If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure. ReadFile does not return until the read operation is complete, and then the system updates the file pointer.
本地 文件 打开 通常 不用 FILE_FLAG_OVERLAPPED
嗯,我知道,FILE_FLAG_OVERLAPPED是异步I/O用的
给各串口写的例子 : int CDload::WriteCommBlock(BYTE *pBlock,int nBlockLen) { //return 0 if error DWORD dwBytesWritten; DWORD dwModemStat; int WaitErr; BOOL fWriteStat; if (!m_bConnected) return 0; dwBytesWritten=0; fWriteStat=WriteFile(m_idComDev,pBlock,nBlockLen,&dwBytesWritten,&m_osWrite); if(!fWriteStat) { // Writting wait if (GetLastError()==ERROR_IO_PENDING) { // if writting processing //??? ResetEvent(m_osWrite.hEvent);//??? if (WaitErr=WaitForSingleObject(m_osWrite.hEvent,60000)) // 1 minute { // WaitErr!=0 if(WaitErr==WAIT_TIMEOUT) { // time out KillTimer(1); // if device error! GetCommModemStatus(m_idComDev,&dwModemStat); if(dwModemStat &= MS_CTS_ON) // if (CTS_ON && TIME_OUT) then COMM Hardware Errors. { // 16 "GREEN" AfxMessageBox("COMM Device hardware errors.",MB_ICONSTOP);// don't move m_bLoadStop=1; dwBytesWritten=0; } else { // 0 "RED" AfxMessageBox("CTS timer 60 seconds out!",MB_ICONSTOP); m_bLoadStop=1; dwBytesWritten=0; } } // if(WaitErr==WAIT_FAILED) =-1 // else other waiterr } else { // WaitErr=0.if you SetEvent(m_osWrite.hEvent) anywhere else GetOverlappedResult(m_idComDev,&m_osWrite,&dwBytesWritten,FALSE); m_osWrite.Offset +=dwBytesWritten; } } // else // not IO_PENDING } return (int)dwBytesWritten; }
和串口读://///////////////////////////////////////////////////// int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen) { //return 0 if error int WaitErr; BOOL fReadStart; COMSTAT ComStat; DWORD dwErrorFlags,dwLength; if (!m_bConnected) return 0; if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat)) { //ComStat filled if (dwErrorFlags) { if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow"); else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error"); else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error"); else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error"); else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected"); else AfxMessageBox("CE_OTHERS"); } } // nBlockLen may >,=,< ComStat.cbInQue ! dwLength=min((DWORD)nBlockLen,ComStat.cbInQue); if(dwLength>0) { // read required fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead); if(!fReadStart) { // if there was a problem, or the async. operation's still pending ... if(GetLastError()==ERROR_IO_PENDING) { // asynchronous i/o is still in progress if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute { // time over if(WaitErr==WAIT_TIMEOUT) { // time out dwLength=0; AfxMessageBox("Time out !"); }// end time out }// end waiterr } // end io_pending else { // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE); m_osRead.Offset +=dwLength; } } } //end if dwLength>0 return dwLength; }
A pointer to an OVERLAPPED structure.This structure is required if hFile is created with FILE_FLAG_OVERLAPPED.If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure.If hFile is created with FILE_FLAG_OVERLAPPED and lpOverlappedis NULL, the function can report incorrectly that the read operation is complete.If hFile is opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure, and ReadFile may return before the read operation is complete. In this scenario, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING, which allows the calling process to continue while the read operation completes. The event specified in the OVERLAPPED structure is set to the signaled state when the read operation is complete, and then the caller must adjust the position of the file pointer.ReadFile resets the event specified by the hEvent member of the OVERLAPPED structure to a nonsignaled state when it begins the I/O operation. Therefore, the caller does not need to do that.If hFile is 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 is complete, and then the system updates the file pointer.If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure. ReadFile does not return until the read operation is complete, and then the system updates the file pointer.
FILE_FLAG_OVERLAPPED
:
int CDload::WriteCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
DWORD dwBytesWritten;
DWORD dwModemStat;
int WaitErr;
BOOL fWriteStat; if (!m_bConnected) return 0;
dwBytesWritten=0;
fWriteStat=WriteFile(m_idComDev,pBlock,nBlockLen,&dwBytesWritten,&m_osWrite);
if(!fWriteStat)
{ // Writting wait
if (GetLastError()==ERROR_IO_PENDING)
{ // if writting processing
//??? ResetEvent(m_osWrite.hEvent);//???
if (WaitErr=WaitForSingleObject(m_osWrite.hEvent,60000)) // 1 minute
{ // WaitErr!=0
if(WaitErr==WAIT_TIMEOUT)
{ // time out
KillTimer(1);
// if device error!
GetCommModemStatus(m_idComDev,&dwModemStat);
if(dwModemStat &= MS_CTS_ON) // if (CTS_ON && TIME_OUT) then COMM Hardware Errors.
{ // 16 "GREEN"
AfxMessageBox("COMM Device hardware errors.",MB_ICONSTOP);// don't move
m_bLoadStop=1;
dwBytesWritten=0;
}
else
{ // 0 "RED"
AfxMessageBox("CTS timer 60 seconds out!",MB_ICONSTOP);
m_bLoadStop=1;
dwBytesWritten=0;
}
}
// if(WaitErr==WAIT_FAILED) =-1
// else other waiterr
}
else
{ // WaitErr=0.if you SetEvent(m_osWrite.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osWrite,&dwBytesWritten,FALSE);
m_osWrite.Offset +=dwBytesWritten;
}
}
// else // not IO_PENDING
}
return (int)dwBytesWritten;
}
int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
int WaitErr;
BOOL fReadStart;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength; if (!m_bConnected) return 0; if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat))
{ //ComStat filled
if (dwErrorFlags)
{
if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow");
else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error");
else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error");
else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error");
else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected");
else AfxMessageBox("CE_OTHERS");
}
}
// nBlockLen may >,=,< ComStat.cbInQue !
dwLength=min((DWORD)nBlockLen,ComStat.cbInQue);
if(dwLength>0)
{ // read required
fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
} //end if dwLength>0
return dwLength;
}
BOOLReadFile(
HANDLEhFile,//文件的句柄
LPVOIDlpBuffer,//用于保存读入数据的一个缓冲区
DWORDnNumberOfBytesToRead,//要读入的字节数
LPDWORDlpNumberOfBytesRead,//指向实际读取字节数的指针
LPOVERLAPPEDlpOverlapped
//如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须,用这个参数引用一个特殊的结构。
//该结构定义了一次异步读取操作。否则,应将这个参数设为NULL
);