willzoe
willzoe
采纳率33.3%
2015-12-07 08:12

程序运行结束后总是显示:Run-Time Check Failure #2

程序运行结束后总是显示:Run-Time Check Failure #2 - Stack around the variable 'cmd_value' was corrupted.

程序过程运行正确,调试发现程序运行结束之后就会报告这个错误。
而且**只有d的数值在0到1(不包括1)**才会发生这样的错误。
我的编程环境是vs2010.

double d=0;
BYTE cmd_star[]="$";
BYTE cmd_cmd[]="001";
BYTE cmd_mid[]=",";
char cmd_value[7];
_gcvt_s(cmd_value,sizeof(cmd_value),d,5);
BYTE cmd_end[]="#";
BYTE cmd2[13];

memcpy(cmd2,cmd_star,sizeof(char));
memcpy(cmd2+1,cmd_cmd,3*sizeof(char));
memcpy(cmd2+4,cmd_mid,sizeof(char));
memcpy(cmd2+5,cmd_value,6*sizeof(char));
memcpy(cmd2+11,cmd_end,sizeof(char));

CWnd * pMain=(CWnd*)AfxGetMainWnd();

Eol.hEvent=CreateEvent(NULL,        
    TRUE,           // manual-reset event 
    FALSE,          // not signaled
    NULL);

if(!::SetCommState(hComFile,&m_BCD))
{
    //AfxMessageBox("SetCommState() error!");
    errmsg = "串口设置失败!";
    p = (LPSTR)(LPCTSTR)errmsg;
    ::SendMessage(pMain->m_hWnd,UM_ERRORMSG,0,(LPARAM)p);
    return ;
}

::EscapeCommFunction(hComFile,SETDTR);

if(WriteData(cmd2,13)!=13)
{
    //AfxMessageBox("Write com error!");
    errmsg = "串口写入失败!";
    p = (LPSTR)(LPCTSTR)errmsg;
    ::SendMessage(pMain->m_hWnd,UM_ERRORMSG,0,(LPARAM)p);
    return;
}
if (Eol.hEvent!=NULL)
{
    ResetEvent(Eol.hEvent);
}

CloseHandle(Eol.hEvent);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

相关推荐