已经使用m_hCom=CreateFile(m_sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if(m_hCom==INVALID_HANDLE_VALUE)
return FALSE;
成功打开串口,使用ReadFile(m_hCom,buf,length,&lengthr,&m_osRead);读取已经打开的串口时错误,错误类型为ERROR_INVALID_HANDLE
请问可能是什么原因,谢谢PS:是一个MDI程序,同样的方法换成SDI程序却正确,难道是单文档程序与多文档程序的区别?

解决方案 »

  1.   

    m_osRead,是一个OVERLAPPED结构,它的hEvent呢?
      

  2.   

    to DentistryDoctor
    m_osRead是OVERLAPPED结构
      

  3.   

    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. 
    =====================================================================================系统要用m_osRead.hEvent!!你在调用ReadFile之前没有创建m_osRead.hEvent吧?那它当然是一个INVALID_HANDLE了。这就是DentistryDoctor(MVP称号也不能顶上缺失的文凭(厚积薄发)) 的意思,代他补充。
      

  4.   

    TO:vcPlayer(我是一只小小鸟!)我已在程序初始化时,创建了m_osRead.hEvent,如下:
    memset(&m_osRead, 0, sizeof(OVERLAPPED));
    if((m_osRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
    return FALSE;
    而且用同样方法在SDI程序中是正确的。哪位朋友能不能给我一个MDI程序,只要最简单的打开串口,用一个副线程监视串口,然后读取串口消息,用消息框输出就行,谢了