如果接口函数调用失败,怎样从返回的HRESULT之中得到错误的原因?

解决方案 »

  1.   

    E_NOINTERFACE The QueryInterface function did not recognize the requested interface. The interface is not supported. 
    E_NOTIMPL The function contains no implementation. 
    E_FAIL An unspecified failure has occurred. 
    E_OUTOFMEMORY The function failed to allocate necessary memory. 
    E_POINTER Invalid pointer. 
    E_INVALIDARG One or more arguments are invalid. 
    E_UNEXPECTED A catastrophic failure has occurred. 
    E_HANDLE Invalid handle. 
    E_ABORT Operation aborted. 
      

  2.   

    使用FormatMessage,看下面这个例子,根据HRESULT向控制台输出对应的错误信息void ErrorMessage(HRESULT hr)
    {
    void* pMsgBuf = NULL;
     
    ::FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    hr,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &pMsgBuf,
    0,
    NULL) ; cout << "Error (" << hex << hr << "):  " 
         << (LPTSTR)pMsgBuf << endl ; // Free the buffer.
    LocalFree(pMsgBuf) ;}
      

  3.   

    1.
    GetLastError()
    2.
    VS: Tools->Error Lookup