为什么我在OnTimer()中定义字符数组程序运行时会出现“该内存不能为read”异常。附代码:
void CTY_VODDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
CString m_Stop_Str;
char m_Stop_Buf[10];
MCIERROR m_Error=mciSendString ("status movie mode",m_Stop_Buf,strlen(m_Stop_Buf),0); 
//l=strlen(m_Stop_Buf);
//m_Stop_Str.Format("%d",m_Stop_Buf);
m_Stop_Str=m_Stop_Buf;
MessageBox(m_Stop_Str);
if(m_Error!=0)
{ KillTimer(1);
MessageBox("出错");
}
if(m_Stop_Str.Left(7)=="stopped")
{
KillTimer(1);
MessageBox(m_Stop_Str);
OnNext();

}
memset(m_Stop_Buf,0,strlen(m_Stop_Buf));
MessageBox("END");
CDialog::OnTimer(nIDEvent);
}

解决方案 »

  1.   

    首先数组应该初始化
    char m_Stop_Buf[10] = {0};
    其次if(m_Stop_Str.Left(7)=="stopped")
     这里需要先判断m_Stop_Str长度是否大于7
    if(m_Error!=0)
     { KillTimer(1);
     MessageBox("出错");
     }
     这个if语句中应该增加return,否则程序会继续执行下去,后续的代码只能带来各种错误。
      

  2.   

    m_Stop_Str=m_Stop_Buf;用format字符串格式化内存不能为read可能是这句造成if(m_Stop_Str.Left(7)=="stopped")
      

  3.   

    感谢楼上的两位。刚才的问题已经解决。现在又出现新问题了
    为什么捕捉不到mci状态呢?每次进入定时器用MessageBox()显示字符串为空
    void CTY_VODDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    CString m_Stop_Str;
    char m_Stop_Buf[256]={0};
    MCIERROR m_Error=mciSendString ("status movie mode",m_Stop_Buf,strlen(m_Stop_Buf),0); 
    m_Stop_Str.Format("%s",m_Stop_Buf);
    MessageBox(m_Stop_Str);
    if(m_Error!=0)
    { KillTimer(1);
    MessageBox("出错");
    }
    if(m_Stop_Str.Left(7)=="stopped")
    {
    KillTimer(1);
    MessageBox(m_Stop_Str);
    OnNext();//如果播放完毕就立即播放下一首。

    }
    CDialog::OnTimer(nIDEvent);
    }