// Given an EXCEPINFO, register the error information with the // IErrorInfo interface. BOOL PyCom_SetCOMErrorFromExcepInfo(const EXCEPINFO *pexcepinfo, REFIID riid) { ICreateErrorInfo *pICEI; HRESULT hr = CreateErrorInfo(&pICEI); if ( SUCCEEDED(hr) ) { pICEI->SetGUID(riid); pICEI->SetHelpContext(pexcepinfo->dwHelpContext); if ( pexcepinfo->bstrDescription ) pICEI->SetDescription(pexcepinfo->bstrDescription); if ( pexcepinfo->bstrHelpFile ) pICEI->SetHelpFile(pexcepinfo->bstrHelpFile); if ( pexcepinfo->bstrSource ) pICEI->SetSource(pexcepinfo->bstrSource); IErrorInfo *pIEI; Py_BEGIN_ALLOW_THREADS hr = pICEI->QueryInterface(IID_IErrorInfo, (LPVOID*) &pIEI); Py_END_ALLOW_THREADS if ( SUCCEEDED(hr) ) { SetErrorInfo(0, pIEI); pIEI->Release(); } pICEI->Release(); } return SUCCEEDED(hr); }
// // // makes an HRESULT with a code based on the DOM error code // HRESULT MakeHRESULT(DOMException& ex) { ICreateErrorInfo* pCErr = NULL; HRESULT sc = CreateErrorInfo(&pCErr); if(SUCCEEDED(sc)) { const XMLCh* msg = ex.msg; if(msg == NULL) { if(ex.code >= DOMException::INDEX_SIZE_ERR && ex.code <= DOMException::VALIDATION_ERR) { sc = pCErr->SetDescription(Msgs[ex.code]); } else { sc = pCErr->SetDescription(Msgs[0]); } } else { sc = pCErr->SetDescription(SysAllocString(ex.msg)); } IErrorInfo* pErr = NULL; sc = pCErr->QueryInterface(IID_IErrorInfo,(void**) &pErr); if(SUCCEEDED(sc)) { sc = SetErrorInfo(0,pErr); pErr->Release(); } pCErr->Release(); } return 0x80040600 + ex.code; }
// The CreateComErrorInfo function uses CreateErrorInfo to create error // object, which is then filled with the error information and passed to // the acceptor by a call to SetErrorInfo. void CreateComErrorInfo(const char* description, const char* source, const char* helpFile, UInt32 helpContext) { ICreateErrorInfo* createErrInfo; IErrorInfo* errInfo; std::basic_string<wchar_t> buf; if (CreateErrorInfo(&createErrInfo) == S_OK) { createErrInfo->SetGUID(IID_IErrorInfo); if (description != NULL) { buf = WideStringFromMultiByteString(description); createErrInfo->SetDescription(const_cast<wchar_t*>(buf.c_str())); } if (source != NULL) { buf = WideStringFromMultiByteString(source); createErrInfo->SetSource(const_cast<wchar_t*>(buf.c_str())); } if (helpFile != NULL) { buf = WideStringFromMultiByteString(helpFile); createErrInfo->SetHelpFile(const_cast<wchar_t*>(buf.c_str())); } createErrInfo->SetHelpContext(helpContext); if (createErrInfo->QueryInterface(IID_IErrorInfo, (void**)(&errInfo)) == S_OK) { SetErrorInfo(0, errInfo); errInfo->Release(); } createErrInfo->Release(); } }
/** * Sets the COM error from a result code and text message. This is the base * implementation for subsequent string based overrides * @param hResult the COM error code to be used * @param message the message to put in the error * @return the error code passed in via hResult */ static HRESULT Error(HRESULT hResult, const CComBSTR & message) { CComPtr<ICreateErrorInfo> pCreateError; CComPtr<IErrorInfo> pError; HRESULT result = CreateErrorInfo(&pCreateError); if(FAILED(result)) return E_NOTIMPL; result = pCreateError->QueryInterface(&pError); if(FAILED(result)) return E_NOTIMPL; result = pCreateError->SetDescription(message); if(FAILED(result)) return E_NOTIMPL; result = pCreateError->SetGUID(IID_IDispatch); if(FAILED(result)) return E_NOTIMPL; CComBSTR source(L"@mozilla.XPCDispatchTearOff"); result = pCreateError->SetSource(source); if(FAILED(result)) return E_NOTIMPL; result = SetErrorInfo(0, pError); if(FAILED(result)) return E_NOTIMPL; return hResult; }
STDMETHODIMP nsXPCDispTestMethods::CreateError() { CComBSTR someText(L"CreateError Test"); ICreateErrorInfo * pCreateError; IErrorInfo * pError; HRESULT result = CreateErrorInfo(&pCreateError); if (FAILED(result)) return E_NOTIMPL; result = pCreateError->QueryInterface(&pError); if (FAILED(result)) return E_NOTIMPL; result = pCreateError->SetDescription(someText); if (FAILED(result)) return E_NOTIMPL; result = pCreateError->SetGUID(IID_nsIXPCDispTestMethods); if (FAILED(result)) return E_NOTIMPL; CComBSTR source(L"XPCIDispatchTest.nsXPCDispTestMethods.1"); result = pCreateError->SetSource(source); if (FAILED(result)) return E_NOTIMPL; result = SetErrorInfo(0, pError); if (FAILED(result)) return E_NOTIMPL; pError->Release(); pCreateError->Release(); return E_FAIL; }
HRESULT COMError::GenerateError(HM::String sDescription) { if (sDescription == _T("")) { sDescription = "The operation failed. Please check the hMailServer log for details."; } // Get the ICreateErrorInfo Interface ICreateErrorInfo *pCreateErrorInfo = NULL; HRESULT hSuccess = CreateErrorInfo(&pCreateErrorInfo); ATLASSERT(SUCCEEDED(hSuccess)); // pCreateErrorInfo->SetGUID(CLSID_BaseApp); pCreateErrorInfo->SetDescription(sDescription.AllocSysString()); HM::String sSource = "hMailServer COM library"; pCreateErrorInfo->SetSource(sSource.AllocSysString()); IErrorInfo *pErrInfo; if (SUCCEEDED(pCreateErrorInfo->QueryInterface(IID_IErrorInfo, reinterpret_cast<void**>(&pErrInfo)))) { SetErrorInfo(0, pErrInfo); pErrInfo->Release(); } pCreateErrorInfo->Release(); return MAKE_HRESULT(1, FACILITY_ITF, 1001); }
void SetErrorInfo(HRESULT hr, const wchar_t* err) { ICreateErrorInfoPtr errCreate; if (SUCCEEDED(CreateErrorInfo(&errCreate)) && SUCCEEDED(errCreate->SetDescription(const_cast<wchar_t*>(err))) && SUCCEEDED(errCreate->SetSource(const_cast<wchar_t*>(g_COCLASS_PROGIDW))) ) { IErrorInfoPtr errorInfo = errCreate; SetErrorInfo(0, errorInfo); } }
__declspec(noreturn) void ThrowComError(HRESULT hr, LPOLESTR err) { ICreateErrorInfoPtr errCreate; if (SUCCEEDED(CreateErrorInfo(&errCreate)) && SUCCEEDED(errCreate->SetDescription(const_cast<wchar_t*>(err))) && SUCCEEDED(errCreate->SetSource(const_cast<wchar_t*>(g_COCLASS_PROGIDW))) ) { IErrorInfoPtr errorInfo = errCreate; throw _com_error(hr, errorInfo.Detach()); } throw _com_error(hr); }
//--------------------------------------------------------------------------- // RaiseException // // Raises an exception using the given source and description //--------------------------------------------------------------------------- void CAdRotator::RaiseException ( LPOLESTR strDescr ) { HRESULT hr; ICreateErrorInfo *pICreateErr; IErrorInfo *pIErr; LANGID langID = LANG_NEUTRAL; /* * Thread-safe exception handling means that we call * CreateErrorInfo which gives us an ICreateErrorInfo pointer * that we then use to set the error information (basically * to set the fields of an EXCEPINFO structure. We then * call SetErrorInfo to attach this error to the current * thread. ITypeInfo::Invoke will look for this when it * returns from whatever function was invokes by calling * GetErrorInfo. */ WCHAR strSource[MAX_RESSTRINGSIZE]; if ( ::LoadStringW( _Module.GetResourceInstance(), IDS_ERROR_SOURCE, strSource, MAX_RESSTRINGSIZE ) > 0 ) { //Not much we can do if this fails. if (!FAILED(CreateErrorInfo(&pICreateErr))) { pICreateErr->SetGUID(CLSID_AdRotator); pICreateErr->SetHelpFile(L""); pICreateErr->SetHelpContext(0L); pICreateErr->SetSource(strSource); pICreateErr->SetDescription(strDescr); hr = pICreateErr->QueryInterface(IID_IErrorInfo, (void**)&pIErr); if (SUCCEEDED(hr)) { if(SUCCEEDED(SetErrorInfo(0L, pIErr))) { pIErr->Release(); } } } pICreateErr->Release(); } }
void TkWinSend_SetExcepInfo( Tcl_Interp *interp, EXCEPINFO *pExcepInfo) { Tcl_Obj *opError, *opErrorInfo, *opErrorCode; ICreateErrorInfo *pCEI; IErrorInfo *pEI, **ppEI = &pEI; HRESULT hr; if (!pExcepInfo) { return; } opError = Tcl_GetObjResult(interp); opErrorInfo = Tcl_GetVar2Ex(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY); opErrorCode = Tcl_GetVar2Ex(interp, "errorCode", NULL, TCL_GLOBAL_ONLY); /* * Pack the trace onto the end of the Tcl exception descriptor. */ opErrorCode = Tcl_DuplicateObj(opErrorCode); Tcl_IncrRefCount(opErrorCode); Tcl_ListObjAppendElement(interp, opErrorCode, opErrorInfo); /* TODO: Handle failure to append */ pExcepInfo->bstrDescription = SysAllocString(Tcl_GetUnicode(opError)); pExcepInfo->bstrSource = SysAllocString(Tcl_GetUnicode(opErrorCode)); Tcl_DecrRefCount(opErrorCode); pExcepInfo->scode = E_FAIL; hr = CreateErrorInfo(&pCEI); if (!SUCCEEDED(hr)) { return; } hr = pCEI->lpVtbl->SetGUID(pCEI, &IID_IDispatch); hr = pCEI->lpVtbl->SetDescription(pCEI, pExcepInfo->bstrDescription); hr = pCEI->lpVtbl->SetSource(pCEI, pExcepInfo->bstrSource); hr = pCEI->lpVtbl->QueryInterface(pCEI, &IID_IErrorInfo, (void **) ppEI); if (SUCCEEDED(hr)) { SetErrorInfo(0, pEI); pEI->lpVtbl->Release(pEI); } pCEI->lpVtbl->Release(pCEI); }
/*---------------------------------------------------------------------------------------------- Creates an error object and then sets a description from a resource id. Also sets a full help URL as required by HtmlHelp. Uses ierr as an index for both resource id and help URL. @param ierr Index to a set of htm help files (second part of full help URL) and matching resource strings for Message Box text. @param pei [out] Error info object ----------------------------------------------------------------------------------------------*/ void LgKeymanHandler::ThrowErrorWithInfo(HRESULT hrErr, int stidDescription) { IErrorInfoPtr qei; ICreateErrorInfoPtr qcei; // Create error info object. CheckHr(CreateErrorInfo(&qcei)); StrUni stu(stidDescription); CheckHr(qcei->SetDescription((wchar *)stu.Chars())); // Now get the IErrorInfo interface of the error object and set it for the current thread. CheckHr(qcei->QueryInterface(IID_IErrorInfo, (void **)&qei)); SetErrorInfo(0, qei); ThrowHr(hrErr, stu.Chars(), -1, qei); // An error object exists. }
void SetExcepInfo( Tcl_Interp* interp, EXCEPINFO *pExcepInfo) { if (pExcepInfo) { Tcl_Obj *opError, *opErrorInfo, *opErrorCode; ICreateErrorInfo *pCEI; IErrorInfo *pEI, **ppEI = &pEI; HRESULT hr; opError = Tcl_GetObjResult(interp); opErrorInfo = Tcl_GetVar2Ex(interp, "errorInfo",NULL, TCL_GLOBAL_ONLY); opErrorCode = Tcl_GetVar2Ex(interp, "errorCode",NULL, TCL_GLOBAL_ONLY); if (Tcl_IsShared(opErrorCode)) { Tcl_Obj *ec = Tcl_DuplicateObj(opErrorCode); Tcl_IncrRefCount(ec); Tcl_DecrRefCount(opErrorCode); opErrorCode = ec; } Tcl_ListObjAppendElement(interp, opErrorCode, opErrorInfo); pExcepInfo->bstrDescription = SysAllocString(Tcl_GetUnicode(opError)); pExcepInfo->bstrSource = SysAllocString(Tcl_GetUnicode(opErrorCode)); pExcepInfo->scode = E_FAIL; hr = CreateErrorInfo(&pCEI); if (SUCCEEDED(hr)) { hr = pCEI->lpVtbl->SetGUID(pCEI, &IID_IDispatch); hr = pCEI->lpVtbl->SetDescription(pCEI, pExcepInfo->bstrDescription); hr = pCEI->lpVtbl->SetSource(pCEI, pExcepInfo->bstrSource); hr = pCEI->lpVtbl->QueryInterface(pCEI, &IID_IErrorInfo, (void**) ppEI); if (SUCCEEDED(hr)) { SetErrorInfo(0, pEI); pEI->lpVtbl->Release(pEI); } pCEI->lpVtbl->Release(pCEI); } } }
HRESULT PASCAL CComObjectRoot::Error(const CLSID& clsid, LPCOLESTR lpszDesc, const IID& iid, HRESULT hRes) { CComPtr<ICreateErrorInfo> pICEI; if (SUCCEEDED(CreateErrorInfo(&pICEI))) { CComPtr<IErrorInfo> pErrorInfo; pICEI->SetGUID(iid); LPOLESTR lpsz; ProgIDFromCLSID(clsid, &lpsz); if (lpsz != NULL) pICEI->SetSource(lpsz); CoTaskMemFree(lpsz); pICEI->SetDescription((LPOLESTR)lpszDesc); if (SUCCEEDED(pICEI->QueryInterface(IID_IErrorInfo, (void**)&pErrorInfo))) SetErrorInfo(0, pErrorInfo); } return (hRes == 0) ? DISP_E_EXCEPTION : hRes; }
void MyActiveSite::Throw(TCHAR* errmsg, REFGUID refguid){ LocRef<ICreateErrorInfo> pcerrinfo; LocRef<IErrorInfo> perrinfo; SysStr errMsg(errmsg); HRESULT hr = CreateErrorInfo(&pcerrinfo); if (FAILED(hr)) return; pcerrinfo->SetDescription(errMsg); pcerrinfo->SetGUID(refguid); hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo); if (FAILED(hr)) return; SetErrorInfo(0, perrinfo); }
template <class T> HRESULT SepaControlDispatch<T>::SetErrorInfo(const wchar_t *pMessage) const { ICreateErrorInfo *pCreateErrorInfo; IErrorInfo *pErrorInfo; if (SUCCEEDED(CreateErrorInfo(&pCreateErrorInfo))) { pCreateErrorInfo->SetDescription(_bstr_t(pMessage).Detach()); pCreateErrorInfo->SetGUID(__uuidof(this)); pCreateErrorInfo->SetSource(_bstr_t(TEXT("Separatista")).Detach()); if (SUCCEEDED(pCreateErrorInfo->QueryInterface(IID_IErrorInfo, (LPVOID*)&pErrorInfo))) { ::SetErrorInfo(0, pErrorInfo); pErrorInfo->Release(); } pCreateErrorInfo->Release(); } return DISP_E_EXCEPTION; }
HRESULT ComErrorInfo(const CLSID& clsid, REFIID riid, IErrorInfo ** ppErrInfo, LPCTSTR pszDesc) { USES_CONVERSION; ICreateErrorInfo* pcei = NULL; IErrorInfo * pei = NULL; LPOLESTR lpsz; if(ppErrInfo == NULL) { return E_POINTER; } *ppErrInfo = NULL; if(clsid == CLSID_NULL) { return S_FALSE; } HRESULT hr = CreateErrorInfo(&pcei); if(FAILED(hr)) { return hr; } ProgIDFromCLSID(clsid, &lpsz); if (lpsz != NULL) { pcei->SetSource(lpsz); ::CoTaskMemFree(lpsz); } lpsz = T2OLE(pszDesc); pcei->SetDescription(lpsz); //Interface ID of the interface whose method is throwing the Except. pcei->SetGUID(riid); hr = pcei->QueryInterface(IID_IErrorInfo, (void**) &pei); if(SUCCEEDED(hr)) { *ppErrInfo = pei; } pcei->Release(); return hr; }
HRESULT CCommonUtils::SetErrorCode(HRESULT hr) { if(FAILED(hr)) { CComPtr<ICreateErrorInfo> createErrorInfo; if(SUCCEEDED(CreateErrorInfo(&createErrorInfo))) { CComPtr<IErrorInfo> errorInfo; createErrorInfo->SetDescription(ErrorToString(hr)); createErrorInfo->SetGUID(GUID_NULL); createErrorInfo->SetHelpContext(0); createErrorInfo->SetHelpFile(NULL); createErrorInfo->SetSource(NULL); errorInfo = createErrorInfo; SetErrorInfo(0, errorInfo); } } return hr; }
void CMarshalErrorInfo::GenerateErrorInfo(REFGUID rGUID, _bstr_t source, _bstr_t message) { ICreateErrorInfo* pCreateInfo = NULL; IErrorInfo* pErrorInfo = NULL; // 创建ICreateErrorInfo对象 CreateErrorInfo(&pCreateInfo); // 设置属性值 pCreateInfo->SetGUID(rGUID); pCreateInfo->SetSource(source); pCreateInfo->SetDescription(message); // 获得pCreateInfo的IErrorInfo接口引用 pCreateInfo->QueryInterface(IID_IErrorInfo, (void**)&pErrorInfo); // 设置IErrorInfo SetErrorInfo(0,pErrorInfo); // 释放对象 pErrorInfo->Release(); pCreateInfo->Release(); }
HRESULT DualHandleException(REFIID riidSource, const CException* pAnyException) { USES_CONVERSION; ASSERT_VALID(pAnyException); TRACE0("DualHandleException called\n"); // Set ErrInfo object so that VTLB binding container // applications can get rich error information. ICreateErrorInfo* pcerrinfo; HRESULT hr = CreateErrorInfo(&pcerrinfo); if (SUCCEEDED(hr)) { TCHAR szDescription[256]; LPCTSTR pszDescription = szDescription; GUID guid = GUID_NULL; DWORD dwHelpContext = 0; BSTR bstrHelpFile = NULL; BSTR bstrSource = NULL; if (pAnyException->IsKindOf(RUNTIME_CLASS(COleDispatchException))) { // specific IDispatch style exception COleDispatchException* e = (COleDispatchException*)pAnyException; guid = riidSource; hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, (e->m_wCode + 0x200)); pszDescription = e->m_strDescription; dwHelpContext = e->m_dwHelpContext; // propagate source and help file if present // call ::SysAllocString directly so no further exceptions are thrown if (!e->m_strHelpFile.IsEmpty()) bstrHelpFile = ::SysAllocString(T2COLE(e->m_strHelpFile)); if (!e->m_strSource.IsEmpty()) bstrSource = ::SysAllocString(T2COLE(e->m_strSource)); } else if (pAnyException->IsKindOf(RUNTIME_CLASS(CMemoryException))) { // failed memory allocation AfxLoadString(AFX_IDP_FAILED_MEMORY_ALLOC, szDescription); hr = E_OUTOFMEMORY; } else { // other unknown/uncommon error AfxLoadString(AFX_IDP_INTERNAL_FAILURE, szDescription); hr = E_UNEXPECTED; } if (bstrHelpFile == NULL && dwHelpContext != 0) bstrHelpFile = ::SysAllocString(T2COLE(AfxGetApp()->m_pszHelpFilePath)); if (bstrSource == NULL) bstrSource = ::SysAllocString(T2COLE(AfxGetAppName())); // Set up ErrInfo object pcerrinfo->SetGUID(guid); pcerrinfo->SetDescription(::SysAllocString(T2COLE(pszDescription))); pcerrinfo->SetHelpContext(dwHelpContext); pcerrinfo->SetHelpFile(bstrHelpFile); pcerrinfo->SetSource(bstrSource); TRACE(_T("\tSource = %ws\n"), bstrSource); TRACE(_T("\tDescription = %s\n"), pszDescription); TRACE(_T("\tHelpContext = %lx\n"), dwHelpContext); TRACE(_T("\tHelpFile = %ws\n"), bstrHelpFile); // Set the ErrInfo object for the current thread IErrorInfo* perrinfo; if (SUCCEEDED(pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID*)&perrinfo))) { SetErrorInfo(0, perrinfo); perrinfo->Release(); } pcerrinfo->Release(); } TRACE(_T("DualHandleException returning HRESULT %lx\n"), hr); return hr; }
static void test_error_info(void) { HRESULT hr; ICreateErrorInfo *pCreateErrorInfo; IErrorInfo *pErrorInfo; static WCHAR wszDescription[] = {'F','a','i','l','e','d',' ','S','p','r','o','c','k','e','t',0}; static WCHAR wszHelpFile[] = {'s','p','r','o','c','k','e','t','.','h','l','p',0}; static WCHAR wszSource[] = {'s','p','r','o','c','k','e','t',0}; IUnknown *unk; hr = CreateErrorInfo(&pCreateErrorInfo); ok_ole_success(hr, "CreateErrorInfo"); hr = ICreateErrorInfo_QueryInterface(pCreateErrorInfo, &IID_IUnknown, (void**)&unk); ok_ole_success(hr, "QI"); IUnknown_Release(unk); hr = ICreateErrorInfo_SetDescription(pCreateErrorInfo, NULL); ok_ole_success(hr, "ICreateErrorInfo_SetDescription"); hr = ICreateErrorInfo_SetDescription(pCreateErrorInfo, wszDescription); ok_ole_success(hr, "ICreateErrorInfo_SetDescription"); hr = ICreateErrorInfo_SetGUID(pCreateErrorInfo, &CLSID_WineTest); ok_ole_success(hr, "ICreateErrorInfo_SetGUID"); hr = ICreateErrorInfo_SetHelpContext(pCreateErrorInfo, 0xdeadbeef); ok_ole_success(hr, "ICreateErrorInfo_SetHelpContext"); hr = ICreateErrorInfo_SetHelpFile(pCreateErrorInfo, NULL); ok_ole_success(hr, "ICreateErrorInfo_SetHelpFile"); hr = ICreateErrorInfo_SetHelpFile(pCreateErrorInfo, wszHelpFile); ok_ole_success(hr, "ICreateErrorInfo_SetHelpFile"); hr = ICreateErrorInfo_SetSource(pCreateErrorInfo, NULL); ok_ole_success(hr, "ICreateErrorInfo_SetSource"); hr = ICreateErrorInfo_SetSource(pCreateErrorInfo, wszSource); ok_ole_success(hr, "ICreateErrorInfo_SetSource"); hr = ICreateErrorInfo_QueryInterface(pCreateErrorInfo, &IID_IErrorInfo, (void **)&pErrorInfo); ok_ole_success(hr, "ICreateErrorInfo_QueryInterface"); hr = IErrorInfo_QueryInterface(pErrorInfo, &IID_IUnknown, (void**)&unk); ok_ole_success(hr, "QI"); IUnknown_Release(unk); ICreateErrorInfo_Release(pCreateErrorInfo); hr = SetErrorInfo(0, pErrorInfo); ok_ole_success(hr, "SetErrorInfo"); IErrorInfo_Release(pErrorInfo); pErrorInfo = NULL; hr = GetErrorInfo(0, &pErrorInfo); ok_ole_success(hr, "GetErrorInfo"); IErrorInfo_Release(pErrorInfo); hr = GetErrorInfo(0, &pErrorInfo); ok(hr == S_FALSE, "GetErrorInfo should have returned S_FALSE instead of 0x%08x\n", hr); ok(!pErrorInfo, "pErrorInfo should be set to NULL\n"); hr = SetErrorInfo(0, NULL); ok_ole_success(hr, "SetErrorInfo"); hr = GetErrorInfo(0xdeadbeef, &pErrorInfo); ok(hr == E_INVALIDARG, "GetErrorInfo should have returned E_INVALIDARG instead of 0x%08x\n", hr); hr = SetErrorInfo(0xdeadbeef, NULL); ok(hr == E_INVALIDARG, "SetErrorInfo should have returned E_INVALIDARG instead of 0x%08x\n", hr); }
// Throws the given error code and the message corresponding to the code. If the code is // a standard code and no message is provided, then the message is extracted from the system. HRESULT CCOMError::DispatchError(HRESULT hError, REFCLSID clsid, LPCTSTR source, LPCTSTR description, DWORD helpContext, LPCTSTR helpFileName) { // This function uses ATL conversion macros // (Hence we must use this MACRO provided by ATL) USES_CONVERSION; // Convert the description to OLE string LPOLESTR wszError = NULL; if(description != NULL) { // Convert to wide char wszError = T2OLE(description); } else { // If the code is a Win32 error code if(HRESULT_FACILITY(hError) == FACILITY_WIN32) { // Get the error from the system LPTSTR szError = NULL; if(!::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, HRESULT_CODE(hError), MAKELANGID(LANG_USER_DEFAULT, SUBLANG_DEFAULT), (LPTSTR)&szError, 0, NULL)) return HRESULT_FROM_WIN32(GetLastError()); // Convert the Error multibyte string to OLE string if(szError != NULL) { // Convert to wide char wszError = T2OLE(szError); // Free the multibyte string LocalFree(szError); } } } // Convert the source string to OLE string LPOLESTR wszSource = NULL; if(source != NULL) wszSource = T2OLE(source); // Convert the help filename to OLE string LPOLESTR wszHelpFile = NULL; if(helpFileName != NULL) wszHelpFile = T2OLE(helpFileName); // Get the ICreateErrorInfo Interface ICreateErrorInfo *pCreateErrorInfo = NULL; HRESULT hSuccess = CreateErrorInfo(&pCreateErrorInfo); ATLASSERT(SUCCEEDED(hSuccess)); // Fill the error information into it pCreateErrorInfo->SetGUID(clsid); if(wszError != NULL) pCreateErrorInfo->SetDescription(wszError); if(wszSource != NULL) pCreateErrorInfo->SetSource(wszSource); if(wszHelpFile != NULL) pCreateErrorInfo->SetHelpFile(wszHelpFile); pCreateErrorInfo->SetHelpContext(helpContext); // Get the IErrorInfo interface IErrorInfo *pErrorInfo = NULL; hSuccess = pCreateErrorInfo->QueryInterface(IID_IErrorInfo, (LPVOID *)&pErrorInfo); if(FAILED(hSuccess)) { pCreateErrorInfo->Release(); return hSuccess; } // Set this error information in the current thread hSuccess = SetErrorInfo(0, pErrorInfo); // Finally release the interfaces pCreateErrorInfo->Release(); pErrorInfo->Release(); // Failed to set the error info! if(FAILED(hSuccess)) return hSuccess; // And, Return the error code that was asked // to be dispatched HRESULT result = MAKE_HRESULT(1, FACILITY_ITF, hError); return result; }
HRESULT ceDispatchSetErrorInfoSub( IN HRESULT hrError, OPTIONAL IN WCHAR const *pwszIDispatchMethod, OPTIONAL IN WCHAR const *pwszDescription, OPTIONAL IN WCHAR const *pwszSource, OPTIONAL IN IID const *piid, OPTIONAL IN WCHAR const *pwszHelpFile, IN DWORD dwHelpFileContext) { HRESULT hr; WCHAR const *pwszError = NULL; ICreateErrorInfo *pCreateErrorInfo = NULL; IErrorInfo *pErrorInfo = NULL; if (NULL != pwszIDispatchMethod) { pwszError = ceGetErrorMessageText(hrError, TRUE); } hr = CreateErrorInfo(&pCreateErrorInfo); _JumpIfError(hr, error, "CreateErrorInfo"); if (NULL != piid) { hr = pCreateErrorInfo->SetGUID(*piid); _PrintIfError(hr, "SetGUID"); } if (NULL != pwszSource) { hr = pCreateErrorInfo->SetSource(const_cast<WCHAR *>(pwszSource)); _PrintIfError(hr, "SetSource"); } if (NULL != pwszDescription) { hr = pCreateErrorInfo->SetDescription( const_cast<WCHAR *>(pwszDescription)); _PrintIfError(hr, "SetDescription"); } if (NULL != pwszHelpFile) { hr = pCreateErrorInfo->SetHelpFile(const_cast<WCHAR *>(pwszHelpFile)); _PrintIfError(hr, "SetHelpFile"); hr = pCreateErrorInfo->SetHelpContext(dwHelpFileContext); _PrintIfError(hr, "SetHelpContext"); } hr = pCreateErrorInfo->QueryInterface( IID_IErrorInfo, (VOID **) &pErrorInfo); _JumpIfError(hr, error, "QueryInterface"); SetErrorInfo(0, pErrorInfo); hr = S_OK; error: if (NULL != pwszError) { LocalFree(const_cast<WCHAR *>(pwszError)); } if (NULL != pErrorInfo) { pErrorInfo->Release(); } if (NULL != pCreateErrorInfo) { pCreateErrorInfo->Release(); } return(hr); }