在网上看到这么一段代码,我关于参数的理解,请高手分析对不对。
请高手指教。int CSerial::ReadData( void *buffer, int limit )
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 ); dwBytesRead = (DWORD) ComStat.cbInQue;//缓冲区里面字符数传给dwBytesRead,比如100个 bReadStatus = ReadFile( m_hIDComDev,
                                 buffer,
                                 dwBytesRead,//一次读取的字符数,100个
                                 &dwBytesRead,//已经读取的字符数,比如可能是90个,80个
                                              //在这里dwBytesRead的值讲被替换成90或者80吗?
                                 &m_OverlappedRead ); if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
} return( (int) dwBytesRead );//这是值是多少呢,是90或者80吗?而不是最初的100了是吧?}

解决方案 »

  1.   

    基本上你的注释理解是正确。因为一次ReadFile,虽然指定了期望读取的字符数,但实际返回的结果不一定是这个值,可能相等,也可能偏小。
    所以后续操作依赖的实际字符数应该是返回的结果dwBytesRead。
      

  2.   

    BOOL WINAPI ReadFile(
      __in         HANDLE hFile,
      __out        LPVOID lpBuffer,
      __in         DWORD nNumberOfBytesToRead,
      __out_opt    LPDWORD lpNumberOfBytesRead,
      __inout_opt  LPOVERLAPPED lpOverlapped
    );lpNumberOfBytesRead 
    A pointer to the variable that receives the number of bytes read.ReadFile sets this value to zero (0) before doing any work or error checking. If this parameter is zero (0) when ReadFile returns TRUE on a named pipe, the other end of the message mode pipe calls the WriteFile function with nNumberOfBytesToWrite set to zero (0).If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.If lpOverlapped is not NULL, lpNumberOfBytesRead can be NULL.If this is an overlapped read operation, you can get the number of bytes read by calling GetOverlappedResult.If hFile is associated with an I/O completion port, you can get the number of bytes read by calling GetQueuedCompletionStatus.If I/O completion ports are used and you are using a callback routine to free the memory that is allocated to the OVERLAPPED structure pointed to by the lpOverlapped parameter, specify NULL as the value of this parameter to avoid a memory corruption problem during the deallocation. This memory corruption problem causes an invalid number of bytes to be returned in this parameter.
      

  3.   

    注意防止缓冲区溢出
    dwBytesRead = min(limit, ComStat.cbInQue); 
      

  4.   


    我是想用第四个参数来判断,数据是否接受完整。
    下面的代码成立吗?我运行的时候不理想。do{
    sleep(150);
    bReadStatus = ReadFile( m_hIDComDev,
                            buffer,
                            dwBytesRead,//一次读取的字符数,100个
                            &dwBytesRead,//已经读取的字符数,比如可能是90个,80个
                                         //在这里dwBytesRead的值讲被替换成90或者80吗?
                            &m_OverlappedRead );
    }while(dwBytesRead!=(DWORD) ComStat.cbInQue)
      

  5.   


    我是想用第四个参数来判断,数据是否接受完整。
    下面的代码成立吗?我运行的时候不理想。
    [code=C/C++]do{
    sleep(150);
    bReadStatus = ReadFile( m_hIDComDev,
                            buffer,
                            dwBytesRead,//一次读取的字符数,100个
                            &dwBytesRead,//已经读取的字符数,比如可能是90个,80个
                                         //在这里dwBytesRead的值讲被替换成90或者80吗?
                            &m_OverlappedRead );
    }while(dwBytesRead!=(DWORD) ComStat.cbInQue)
    [code]