我在练习个通信程序的时候,下面的CreateFile函数老是调试不过去,请高手指点
m_hCom = 
CreateFileW(m_sPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if (m_hCom = INVALID_HANDLE_VALUE){
                DWORD wrongFlag =GetLastError();
return FALSE;
}为什么m_hCom老是创建不成功,我想用GetLastError()看错误代码,但是我不知道怎么把错误代码显示出来。我无法看到错误的类型。就不知道怎么查资料。还请高手解答!
谢谢!

解决方案 »

  1.   

    GetlastError返回的自然是dword
    怎么会不知道类型?
      

  2.   

    DWORD dwError = GetLassError();
    CString str;
    str.Format(_T("Error Code :[%d]"), dwError);
    AfxMessageBox(str);
      

  3.   

    可以用FormatMessage见文本显示出来,具体查MSDN
      

  4.   

    得到错误的返回值后,可以用工具中的error lookup察看错误信息
      

  5.   

    if (m_hCom = INVALID_HANDLE_VALUE)
    if (m_hCom == INVALID_HANDLE_VALUE)
      

  6.   

    我用
    DWORD dwError = GetLassError(); 
    CString str; 
    str.Format(_T("Error Code :[%d]"), dwError); 
    AfxMessageBox(str); 
    看到错误类型了
    返回值是 2
    我在msdn中找了
    是ERROR_FILE_NOT_FOUND 
    她上面是这么解释的
    The system cannot find the file specified.
    但是我不知道错误在哪啊?
    请指点
      

  7.   

    把文件字符串贴出来看看
    szFile = "c:\windows\a.rar"; //错
    szFile = "c:\\windows\\a.rar"; //对
      

  8.   

    将GetLastError获得值格式化输出
           
                    LPVOID lpMsgBuf;
    FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL);
    MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
      

  9.   


    用error lookup查看 或者, AFXMESSAGEBOX("%D", GETLASTERROR());
      

  10.   

    if (m_hCom = INVALID_HANDLE_VALUE){ 
    这条语句导致你的if 始终为真改成if (m_hCom == INVALID_HANDLE_VALUE){ 
      

  11.   

    用MSDN查GetLastError或SetLastError,那么多链接,肯定能找到。
    FormatMessage函数可将错误码转化为具体含义。
    例子:void DisplayErrorInWnd(const std::string& strMessage,
       long lError)
    {
    //在m_pWnd所指窗口中显示错误码信息
    //strMessage:指定显示的信息
    //lError:指定错误码(例如:WSAGetLastError()) char chBuf[20];
    itoa(lError, chBuf, 10);
      
    LPVOID lpvMessageBuffer=NULL; 
    FormatMessage(    
    FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,  
    NULL,     
    lError,//错误码 
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   
    (LPTSTR)&lpvMessageBuffer, 0, NULL); std::string strError=strMessage+";错误码:"+chBuf+"--"+(TCHAR *)lpvMessageBuffer;
    strError.replace(strError.rfind('\n'),1,"");
    DisplayInWnd(strError); LocalFree(lpvMessageBuffer);//释放内存
    }