使用API函数,打开串口,写数据的时候,为什么WriteFile总是返回0,该如何解决

使用API函数,打开串口,写数据的时候,为什么WriteFile总是返回0
本人最近网上 下了个c++ builder例子 并且用VSPM虚拟了一个串口。 
在使用WriteFile()函数时遇到了点问题,就是不管WriteFile()函数成功与否,都返回0值;
运行环境:XP sp3, c++ Builder 6.0,VSPM 。

例子的下载地址:http://www.91tech.net/Article/SoftHardTech/SerialCom/200506/1316.html

关键代码:
C/C++ code
int __stdcall OpenCom(int port)
{
        String comname;
        comname = "COM" + IntToStr(port);
        if (hCom==INVALID_HANDLE_VALUE)
        {
                ShowMessage("Can not open the port !");
                CloseHandle(hCom);
                hCom = 0;
                return 0;
        }
        if(hCom != 0)
              return 0;
        hCom=CreateFile( comname.c_str(), //文件名
                         GENERIC_READ|GENERIC_WRITE,//访问模式允许读写
                         0, //此项必须是0
                         NULL,//无安全参数
                         OPEN_EXISTING,//创建方式
                         FILE_FLAG_OVERLAPPED,//异步工作方式
                         NULL);
        if (hCom==INVALID_HANDLE_VALUE)
        {
                ShowMessage("Can not open the port !");
                CloseHandle(hCom);
                hCom = 0;
                return 0;
        }

        if(!GetCommState(hCom,&dcb)) //获得串口设置并用它填充dcb结构体
                ShowMessage("GetCommState failed");

        if (!SetupComm(hCom,1024,1024)) //设置输入输出缓冲区大小
                ShowMessage("SetupComm failed");

        //设置dcb结构成员变量
        dcb.BaudRate=9600;
        dcb.fParity=0;
        dcb.Parity=NOPARITY;
        dcb.StopBits=ONESTOPBIT;
        dcb.ByteSize=8;
        dcb.fNull=FALSE;

        if(!SetCommState(hCom,&dcb)) //重新配置串口
              ShowMessage("SetCommState failed");


        //设置事件掩码,EV_RXCHAR表示接收一个字符并放到缓冲区划
        if (!SetCommMask(hCom,EV_RXCHAR))
                ShowMessage("SetCommMask failed");


        //创建事件对象,在WaitCommEvent使用
        COMSTAT comstat;
        DWORD dwError=0;
        OverRead.hEvent=CreateEvent(NULL,
                                    true,
                                    false,
                                    NULL);
        ClearCommError(hCom,&dwError,&comstat);

        //清空串口缓冲区,退出所有相关操作
        PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);

        Form1->Caption=comname+" 通信成功!";
        Form1->RadioGroup1->Enabled=false;
     //   Form1->Button1->Enabled=false;
      //  Form1->Button2->Enabled=true;

        //创建线程
        Read232 = new hComThread(false); // (false/true) (run/not run )WatchThread when begin

        return true;
}


void __fastcall SendDatas( char chStr[1024], unsigned long StrLen)
{
         //发送数据
        BOOL WriteState;
      //  unsigned long Written = 0 ;
        LPDWORD lpDword;
        DWORD dwError;

        WriteState = WriteFile(hCom,//用CreateFile 获得的文件句柄
                                chStr,//输出缓冲区首址
                                StrLen,//要求输出的字节数
                                lpDword,//实际输出字节数
                                &OverWrite);//重叠操作方式数据结构地址
        //这里的WriteFile()成功也返回0


                if (!WriteState)//&& GetLastError()== ERROR_IO_PENDING )
        {
             ShowMessage("Error !!!");
        }
        
}



希望各位高手指点迷津。

------解决方案--------------------
完整的写数据代码:
C/C++ code

int ComWin32::PutBuffer(char *buffer, int count)
{
    COMSTAT     queue;
    DWORD       error;
    DWORD       written = 0;
    int         bytesWrite;
    
    ClearCommError(comHandle, &error, &queue);
    bytesWrite = txQueue - queue.cbOutQue;
    if (count < bytesWrite)
        bytesWrite = count;
    
    if (WriteFile(comHandle, buffer, bytesWrite, &written, &ovReadWrite) == 0)
    {
        if ((error = GetLastError()) == ERROR_IO_PENDING)
            ClearCommError(comHandle, &error, &queue);
        else
            written = bytesWrite;
    } /* end of if */
    
    return written;
}