代码如下:
DWORD CTTYDoc::WriteComm(char *buf, DWORD dwLength)
{
BOOL fState;
OVERLAPPED m_osWrite;
DWORD length=dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
   //清除错误条件;确定窗口状态
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
fState=WriteFile(m_hCom,buf,length,&length,&m_osWrite); if(!fState)
{ if(GetLastError()==ERROR_IO_PENDING)
{
GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);// 等待
}
else
length=0;
} return length;
}
问题一、当输入一个字符时,传入的参数正常。length=dwLength,这条语句有问题,虽然dwLength为1,但length却为0。
问题二、不用length这个变量中转,直接用dwLength参数传入。第一次,大意竟然发现了一个问题。第一次改为WriteFile(m_hCom,buf, dwLength,&length,&m_osWrite) dwLength值仍然为1;发现错误后及时更正WriteFile(m_hCom,buf, dwLength,& dwLength,&m_osWrite)没想到dwLength值为0,在参数传入函数体时这个值就是0。
弄不懂上面的两个问题,希望大家帮忙,谢谢!