写串口时writefile函数返回0,写串口失败。但GetLastError也返回0

在Dev C++中编写了一个控制RFID读写器的程序,直接编译在DOS界面中可以正常运行
但是要求写界面,通过Windows API写了一个界面,把对应写串口放进按键事件中就出现了描述中的问题
w=WriteFile(hCom,message2,strlen(message2),&dwBytesWrite,NULL);
errors=GetLastError();
char buf1[10];
_ultoa(errors, buf1, 10);
fp=fopen("error.txt","w");
fprintf(fp,buf1);
fclose(fp);
if(!w)
{
MessageBox(NULL,"写串口失败2","Message",MB_OK|MB_ICONERROR);
return -1;
}

其中打开串口功能是成功的,也就是说在打开串口的按键事件中、writefile函数也是能成功写串口的
想要通过GetLastError来查看到底是为什么错了
但是看到的返回值是0,却讲没错

附全部代码:
#include
#include
#include <windows.h>
#include
#include
#include <stdio.h>
#include <stdlib.h>
using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd, hwndDT;
MSG msg;
RECT rect;
int dtWidth,dtHeight;

memset(&wc,0,sizeof(wc));
wc.cbSize         = sizeof(WNDCLASSEX);
wc.lpfnWndProc     = WndProc;
wc.hInstance     = hInstance;
wc.hCursor         = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc)) {
    MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
    return 0;
}

hwndDT=GetDesktopWindow(); //取桌面句柄 
GetWindowRect(hwndDT,&rect); //取桌面范围 
dtWidth=rect.right-rect.left; //桌面宽度 
dtHeight=rect.bottom-rect.top; //桌面高度 

hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,
    "RFID读写标签功能实现",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
    (dtWidth-640)/2,   /*窗体居中*/ 
    (dtHeight-480)/2,
    640,
    480,
    NULL,NULL,hInstance,NULL);
//MoveWindow(hwnd, (width-640)/2, (height-480)/2, 640, 480, FALSE);
 
if(hwnd == NULL) {
    MessageBox(NULL, "Window Creation Failed!","Error!", MB_ICONEXCLAMATION|MB_OK);
    return 0;
}

while(GetMessage(&msg, NULL, 0, 0) > 0) { 
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i,w,j;
char readBuffer[1000];
char com[10];

char message1[100]="010C00030410002101060000";// write register
char message2[100]="010B000304140701000000";//selecet tag
char message3[100]="010B000304180320";//read one block  [16]-[17]select block [18]-[21]=0000 
char message4[100]="010F000304184221";//write one block [16]-[17]select block [18]-[25]content [26]-[29]=0000
DWORD dwBytesWrite=100;
COMSTAT ComStat;
DWORD dwErrorFlags;
TCHAR str[100];
DWORD nWantRead=100;
DWORD nReadRead;
FILE *fp;
DWORD errors;

int s;


HDC             hdc;

static int      cxChar, cyChar;

static HWND     hwndButton0;
static HWND     hwndButton1;
static HWND     hwndButton2;
static HWND     hwndEditbox0;
static HWND     hwndEditbox1;
static HWND     hwndEditbox2;
static HWND     hwndEditbox3;
string            strXy;
char            x[5], y[5];
char            order[10];
char            buff[4096] = {0};
const int        IDcmdButton1 = 1;
const int        IDeditBox1 = 2;
const int       IDcmdButton2 = 3;
const int        IDeditBox2 = 4;
const int       IDcmdButton0 = 5; 
const int        IDeditBox0 = 6;
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());

HANDLE hCom; 
 
switch (message) {
    case WM_CREATE:
        
        hwndButton0 = CreateWindow ( TEXT("button"), TEXT("打开串口"),
                                    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                       cxChar * 18, cyChar * 0.5,
                                       10 * cxChar, 2.5 * cyChar,
                                       hwnd, (HMENU)IDcmdButton0, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
        if (!hwndButton0) MessageBox(NULL,"创建按钮失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndButton0,SW_SHOW);
        UpdateWindow(hwndButton0);
        
        
        hwndButton2 = CreateWindow ( TEXT("button"), TEXT("运行功能"),
                                    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                       cxChar * 40, cyChar * 9.5,
                                       30 * cxChar, 2.5 * cyChar,
                                       hwnd, (HMENU)IDcmdButton2, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
        if (!hwndButton2) MessageBox(NULL,"创建按钮失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndButton2,SW_SHOW);
        UpdateWindow(hwndButton2);
        
        // 串口输入框    
        hwndEditbox0 = CreateWindow( TEXT("edit"),NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                                       cxChar * 5, cyChar * 0.5,
                                       10 * cxChar, 2.5 * cyChar,
                                    hwnd,(HMENU)IDeditBox0,NULL,NULL);                                    
        if (!hwndEditbox0) MessageBox(NULL,"创建串口输入框失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndEditbox0,SW_SHOW);
        UpdateWindow(hwndEditbox0);
        
        // 功能选择输入框 
        hwndEditbox1 = CreateWindow( TEXT("edit"),NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                                       cxChar * 5, cyChar * 5,
                                       30 * cxChar, 3.5 * cyChar,
                                    hwnd,(HMENU)IDeditBox1,NULL,NULL);                                    
        if (!hwndEditbox1) MessageBox(NULL,"创建功能选择输入框失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndEditbox1,SW_SHOW);
        UpdateWindow(hwndEditbox1);
        
        //指令补全框 
        hwndEditbox2 = CreateWindow( TEXT("edit"),NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                                       cxChar * 40, cyChar * 5,
                                       30 * cxChar, 3.5 * cyChar,
                                    hwnd,(HMENU)IDeditBox2,NULL,NULL);                                    
        if (!hwndEditbox2) MessageBox(NULL,"创建指令补全框失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndEditbox2,SW_SHOW);
        UpdateWindow(hwndEditbox2);
        
        // 消息返回框 
        hwndEditbox3 = CreateWindow( TEXT("edit"),NULL,
                                    WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                                       cxChar * 5, cyChar * 13,
                                       65 * cxChar, 12 * cyChar,
                                    hwnd,(HMENU)IDeditBox2,NULL,NULL);                                    
        if (!hwndEditbox3) MessageBox(NULL,"创建消息返回框失败","Message",MB_OK|MB_ICONERROR);
        ShowWindow(hwndEditbox3,SW_SHOW);
        UpdateWindow(hwndEditbox3);
        return 0 ;
          

    case WM_SIZE:
        //GetWindowRect(hwnd, &rect);
        //MoveWindow(hwndButton, (rect.right-rect.left)/2 - 15*cxChar ,\
        //(rect.bottom-rect.top)/2 - 1.25*cxChar, 30*cxChar, 2.5*cyChar, FALSE);
        return 0;
        
    case WM_COMMAND:
        //各控件的_click()事件 
        switch (LOWORD(wParam)) {
        case 0:
            PostQuitMessage(0);
            break;
        case IDcmdButton0:
            GetWindowText(hwndEditbox0,com,5);
            hCom=CreateFile(com,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
            if(hCom == INVALID_HANDLE_VALUE)
            {
                MessageBox(NULL,"打开串口失败!!","Message",MB_OK|MB_ICONERROR);
                return -1;
            }
            if(!SetupComm(hCom,1024,1024))
            {
                   MessageBox(NULL,"设置缓冲区失败!!","Message",MB_OK|MB_ICONERROR);
                return -1;
            }
            
            COMMTIMEOUTS TimeOuts;
            TimeOuts.ReadIntervalTimeout=100;
            TimeOuts.ReadTotalTimeoutMultiplier=100;
              TimeOuts.ReadTotalTimeoutConstant=1000;
            TimeOuts.WriteTotalTimeoutMultiplier=50;
            TimeOuts.WriteTotalTimeoutConstant=2000;
            SetCommTimeouts(hCom,&TimeOuts);
            PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);

               DCB dcb;

            GetCommState(hCom, &dcb);
            dcb.BaudRate = CBR_9600;    //波特率
            dcb.ByteSize = 8;     // number of bits/byte, 4-8 指定端口当前使用的数据位
            dcb.Parity = 0;     // 0-4=no,odd,even,mark,space 指定端口当前使用的奇偶校验方法
            dcb.StopBits = 1;    //指定端口当前使用的停止位数
            SetCommState(hCom,&dcb); 
            
            w=WriteFile(hCom,message1,strlen(message1),&dwBytesWrite,NULL);
            if(!w)
            {
                MessageBox(NULL,"写串口失败1!!","Message",MB_OK|MB_ICONERROR);
                  return -1;
            }
            Sleep(100);
            PurgeComm(hCom,PURGE_TXCLEAR);
            ClearCommError(hCom,&dwErrorFlags,&ComStat);
               //读缓冲区的数据 
            s=ReadFile(hCom,readBuffer,800,&nReadRead, NULL);
            if(s==0)
            {
                 MessageBox(NULL,"读取串口失败1!!","Message",MB_OK|MB_ICONERROR);
                 return -1; 
            }
            MessageBox(NULL,"打开串口成功","Message",MB_OK);
            break;
        case IDeditBox1:                
            
            break;
        case IDcmdButton2:
            GetWindowText(hwndEditbox1,order,5);            
            if(order[0]=='s'){
                SetWindowText(hwndEditbox3,"询卡");
                try{
                    w=WriteFile(hCom,message2,strlen(message2),&dwBytesWrite,NULL);
                }
                catch(...){
                    fp=fopen("error.txt","w");                    
                    fprintf(fp,"exception");
                    fclose(fp);
                }
            
                
                if(!w)
                {
                    
                    MessageBox(NULL,"写串口失败2","Message",MB_OK|MB_ICONERROR);
                      return -1;
                }
        
                ClearCommError(hCom,&dwErrorFlags,&ComStat);
                //读缓冲区的数据 
                s=ReadFile(hCom,readBuffer,800,&nReadRead, NULL);
                if(s==0)
                {

                    MessageBox(NULL,"读取串口失败2","Message",MB_OK|MB_ICONERROR);
                     return -1; 
                }
                for(i=0;i<nReadRead;i++){
        
                    if(readBuffer[i]=='[' && readBuffer[i+1]!=',')
                    {
                        SetWindowText(hwndEditbox3,"RFID:");
                        SetWindowText(hwndEditbox3,readBuffer);
                        fp=fopen("RFID.txt","w");
    
                        for(j=i+1;readBuffer[j]!=',';j++)
                        {
                            fputc(readBuffer[j],fp);
                        }
                        fclose(fp);
                        putchar('\n');
                        break;
                    }
                }    
                PurgeComm(hCom,PURGE_RXCLEAR);    
            }
            else if(order[0]=='r'){
                SetWindowText(hwndEditbox3,"读单块");
                GetWindowText(hwndEditbox2,buff,7);
                for(i=0;i<6;i++){
                    message3[16+i]=buff[i];
                }
                SetWindowText(hwndEditbox3,message3);
                
                w=WriteFile(hCom,message3,strlen(message3),&dwBytesWrite,NULL);
                if(!w)
                {
                    MessageBox(NULL,"写串口失败2","Message",MB_OK|MB_ICONERROR);
                      return -1;
                }
                Sleep(1000);
                PurgeComm(hCom,PURGE_TXCLEAR);
                ClearCommError(hCom,&dwErrorFlags,&ComStat);
                //读缓冲区的数据 
                s=ReadFile(hCom,readBuffer,800,&nReadRead, NULL);
                PurgeComm(hCom,PURGE_RXCLEAR);
                if(s==0)
                {
                     MessageBox(NULL,"读取串口失败2","Message",MB_OK|MB_ICONERROR);
                     return -1; 
                }
                for(i=0;i<nReadRead;i++)
                    if(readBuffer[i]=='[' && readBuffer[i+1]!=',')
                    {
                        SetWindowText(hwndEditbox3,"block:");
                        SetWindowText(hwndEditbox3,readBuffer);
                        fp=fopen("block.txt","w");
    
                        for(j=i+1;readBuffer[j]!=',';j++)
                        {
                            fputc(readBuffer[j],fp);
                        }
                        fclose(fp);
                        break;
                    }                     
                } 
                else if(order[0]=='w'){
                SetWindowText(hwndEditbox3,"写单块");
                GetWindowText(hwndEditbox2,buff,15);
                for(i=0;i<14;i++){
                    message4[16+i]=buff[i];
                } 
                SetWindowText(hwndEditbox3,"指令补全完成");
                     
                w=WriteFile(hCom,message4,strlen(message4),&dwBytesWrite,NULL);
                if(!w)
                {
                    MessageBox(NULL,"写串口失败2","Message",MB_OK|MB_ICONERROR);
                      return -1;
                }
                Sleep(1000);
                PurgeComm(hCom,PURGE_TXCLEAR);
                ClearCommError(hCom,&dwErrorFlags,&ComStat);
                SetWindowText(hwndEditbox3,"写串口成功");
                }
            break;
        case IDeditBox2:
            break;
        
        }
        
        return 0; 



    /*case WM_CLOSE:
        if (IDYES==MessageBox(hwnd, "是否真的要退出?", "确认", MB_ICONQUESTION | MB_YESNO))
            DestroyWindow(hwnd);  //销毁窗口
        return 0;*/
      
}

return DefWindowProc(hwnd, message, wParam, lParam);

}

你用try catch查看具体报错。