void PlayMedia(const char *pszFileName) throw(_com_error) { HRESULT hr; IGraphBuilderPtr pGraphBuilder; IMediaControlPtr pMediaControl; IMediaEventPtr pMediaEvent; WCHAR wszFileName[MAX_PATH]; LONG lEventCode; CoInitializeEx(NULL, COINIT_MULTITHREADED); pGraphBuilder.CreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC); wsprintfW(wszFileName, L"%S", pszFileName); hr = pGraphBuilder->RenderFile(wszFileName, NULL); if (!SUCCEEDED(hr)) _com_raise_error(hr); pMediaControl = pGraphBuilder; pMediaEvent = pGraphBuilder; hr = pMediaControl->Run(); if (!SUCCEEDED(hr)) _com_raise_error(hr); hr = pMediaEvent->WaitForCompletion(INFINITE, &lEventCode); if (!SUCCEEDED(hr)) _com_raise_error(hr); }
size_t getChildrenCount(MSXML2::IXMLDOMNodePtr& node) { MSXML2::IXMLDOMNodeListPtr children; const HRESULT getChildNodesRes = node->get_childNodes(&children); if (FAILED(getChildNodesRes)) { _com_raise_error(getChildNodesRes); } LONG chilrenCount = 0; const HRESULT getChildenCountRes = children->get_length(&chilrenCount); if (FAILED(getChildenCountRes)) { _com_raise_error(getChildenCountRes); } return chilrenCount; }
void setNodeText(MSXML2::IXMLDOMNodePtr& node, const String& text) { _bstr_t nodeText(text.c_str()); const HRESULT putTextRes = node->put_text(nodeText); if (FAILED(putTextRes)) { _com_raise_error(putTextRes); } }
bool IEToolbar::onSidebarCmd() { const String friendsSidebarClassID = toString(CLSID_IESidebar); variant_t friendsSideBarGuid = toBSTR(friendsSidebarClassID); const BOOL sideBarIsOpen = RuntimeContext::isSidebarOpened(); variant_t showFlag(sideBarIsOpen ? VARIANT_FALSE : VARIANT_TRUE, VT_BOOL); IWebBrowser2Ptr browser = getWebBrowser(); const HRESULT showSidebarReas = browser->ShowBrowserBar(&friendsSideBarGuid, &showFlag, 0); if (FAILED(showSidebarReas)) { _com_raise_error(showSidebarReas); } // Update the filter on sidebar if we are opening the sidebar, // if it was opened and we are closing it - no if (!sideBarIsOpen) { String filterText = searchEdit_.getEnteredText(); if (filterText == searchEdit_.getPromptText()) { filterText = _T(""); } HWND sideBarHwnd = RuntimeContext::getSidebarHwnd(); if (sideBarHwnd) { String* filterMessageParam = new String(filterText); ::PostMessage(sideBarHwnd, TBM_FILTER_CHANGED, 0, (LPARAM)filterMessageParam); } } return true; }
void CVLDatabase::OpenRecordset(CursorTypeEnum cursorType, bool bind, LockTypeEnum lockType, CursorLocationEnum cursorLocation) { CloseRecordset(); recordset->CursorLocation = cursorLocation; recordset->Open((_bstr_t)recordsetName.c_str(), _variant_t((IDispatch*)connection, true), cursorType, lockType, adCmdTableDirect); opened = true; if (bind) { HRESULT hr = S_OK; hr = recordset->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*)&recordBinding); if (FAILED(hr)) _com_raise_error(hr); hr = recordBinding->BindToRecordset(&record); if (FAILED(hr)) _com_raise_error(hr); } }
void _HR(HRESULT x, const char *filename, int line) { if (FAILED(x)) { ZZLAB_ERROR(filename << '@' << line << ", hr=0x" << std::hex << x); _com_raise_error(x); } }
IUnknownPtr IEToolbar::getControlUnknown() { // Query IUnknown interface. IUnknown* unknown = 0; const HRESULT queryResult = QueryInterface(IID_IUnknown, reinterpret_cast<void**>(&unknown)); if (FAILED(queryResult)) { _com_raise_error(queryResult); } return unknown; }
bool IEToolbar::onToolbarKillFocus(const NMTOOLBAR& messageInfo, LRESULT& lResult) { UNREFERENCED_PARAMETER(messageInfo); UNREFERENCED_PARAMETER(lResult); const IUnknownPtr control = getControlUnknown(); const HRESULT chageFocusResult = site_->OnFocusChangeIS(control, FALSE); if (FAILED(chageFocusResult)) { _com_raise_error(chageFocusResult); } return true; }
void CVLDatabase::UpdateRecord(int index) { SetRecord(index); FindRecord(allItems[index]->id); try { HRESULT hr = recordBinding->Update(&record); if (FAILED(hr)) _com_raise_error(hr); } catch (_com_error &e) { ComErrorToException(e, string("Can not update record in the database")); } }
void TestPropSheetExt::FileNotFound() { try { TestExecutePropSheetExt(_T("filenotfound.vvv")); } catch (const _com_error&) { // expect failed HRESULT. return; } _com_raise_error(E_FAIL); }
/** * throws a _com_error exception with a created error information * object supporting the interface IErrorInfo. The error information * object can be used to determine the source of the error and the * cause of the error. If the HRESULT of the error passed is E_OUT- * OFMEMORY indicating an out of memory exception only a _com_error * exception will be generated without an error information object. * Further if the passed CLSID reference is CLSID_NULL only a _com_error * exception without a error information object will be generated. * The error description (IErrorInfo::SetDescription etc.) will be * generated through a call to ::FormatMessage with the HRESULT. * @param clsid - CLSID indicating the source of the error. * @param riid - Interface context of the error source. * @param hr - HRESULT error code of the error * @param ... additional parameters passed to FormatMessage to * generate the error description.<br> * @return - * @exception _com_error (with error info object). * @see SDK Documentation on ::FormatMessage, interface IErrorInfo. */ void ComThrow(const CLSID& clsid, REFIID riid,HRESULT hr,...) throw() { IErrorInfo* pInfo; LPTSTR pszOut; //special handling for E_OUTOFMEMORY if(hr == E_OUTOFMEMORY) { _com_raise_error(hr); //no error info object } { CString strOut; va_list arg_ptr; va_start(arg_ptr,hr); pszOut = strOut.GetBuffer(_MAX_PATH); FormatMsgArg(pszOut,hr,arg_ptr); strOut.ReleaseBuffer(); va_end(arg_ptr); ComErrorInfo(clsid,riid,&pInfo,strOut); } _com_raise_error(hr,pInfo); }
// 向CAD窗口发送命令 void SendMessageToCad(CString& strCommand, CStringArray& strParamArray) { MSXML2::IXMLDOMDocumentPtr lpDocument; long hr = lpDocument.CreateInstance(__uuidof(DOMDocument)); if ( FAILED(hr) ) _com_raise_error(hr); MSXML2::IXMLDOMProcessingInstructionPtr lpInstruction = lpDocument->createProcessingInstruction("xml","version='1.0' encoding='GB2312'"); lpDocument->appendChild(lpInstruction); MSXML2::IXMLDOMElementPtr lpRoot = lpDocument->createElement(LPCTSTR("root")); lpDocument->appendChild(lpRoot); // 数据类型:命令或数据 MSXML2::IXMLDOMElementPtr lpCommand = lpDocument->createElement("command"); lpCommand->setAttribute("name", _variant_t(strCommand)); lpRoot->appendChild(lpCommand); // 参数列表 for (int i=0; i<strParamArray.GetSize(); i++) { CString strParam = strParamArray.GetAt(i); CString strName = strParam.Left(strParam.Find('=')); CString strValue = strParam.Right(strParam.GetLength()-strParam.Find('=')-1); MSXML2::IXMLDOMElementPtr lpParam = lpDocument->createElement("param"); lpParam->setAttribute("name", _variant_t(strName)); lpParam->setAttribute("value", _variant_t(strValue)); lpCommand->appendChild(lpParam); } CString strXML = (LPCTSTR)(lpDocument->xml); // OutputDebugString(lpDocument->xml); COPYDATASTRUCT cds; cds.dwData = 0; cds.cbData = strXML.GetLength()+1; cds.lpData = (void*)strXML.GetBuffer(0); strXML.ReleaseBuffer(); ::SendMessage(acedGetAcadFrame()->m_hWnd, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds); }
int CVLDatabase::Add(const void *features, DWORD size, LPCTSTR faceId) { const int index = AddItem(0, features, size, faceId, (DWORD) strlen(faceId)); if (opened) { SetRecord(index); try { HRESULT hr = recordBinding->AddNew(&record); if (FAILED(hr)) _com_raise_error(hr); allItems[index]->id = record.id; } catch(_com_error &e) { ComErrorToException(e, string("Can not add record to the database")); } } return index; }
void CreateToolbars() { if (Glb_toolbarArray.GetCount() > 0) return; IAcadMenuGroup *mnuGrp = NULL; if (!getAcadMenuGroup(&mnuGrp)) return ; IAcadToolbars *tlbrs = NULL; HRESULT hr = S_OK; hr = mnuGrp->get_Toolbars(&tlbrs); mnuGrp->Release(); // 得到arx所在路径 TCHAR lpPath[MAX_PATH] = {0}; GetModuleFileName(GetModuleHandle("DistToolBar.arx"), lpPath, MAX_PATH); PathRemoveFileSpec(lpPath); PathAddBackslash(lpPath); CString strXmlFile = lpPath; strXmlFile += "toolbar.xml"; // 得到icon路径 PathAppend(lpPath, "icon"); PathAddBackslash(lpPath); MSXML2::IXMLDOMDocumentPtr lpDocument; hr = lpDocument.CreateInstance(__uuidof(DOMDocument)); if ( FAILED(hr) ) _com_raise_error(hr); VARIANT_BOOL isSuccessful; // 装载XML字符串 if (!lpDocument->load(_variant_t(strXmlFile))) { OutputDebugString("LoadXML failed!"); return; } MSXML2::IXMLDOMElementPtr lpDocElement = NULL; lpDocument->get_documentElement(&lpDocElement); // toolbar list MSXML2::IXMLDOMNodeListPtr lpToolbarList = lpDocElement->getElementsByTagName(_bstr_t("toolbar")); MSXML2::IXMLDOMNodePtr lpToolbar = NULL; for ( ; (lpToolbar = lpToolbarList->nextNode()) != NULL ; ) { MSXML2::IXMLDOMNodePtr lpCaptionNode = lpToolbar->attributes->getNamedItem("caption"); CString strCaption = (char*)lpCaptionNode->text; OutputDebugString(strCaption); // 添加工具条 IAcadToolbar *tlbr = NULL; hr = tlbrs->Add(CComBSTR(strCaption), &tlbr); if FAILED(hr) continue; Glb_toolbarArray.Add(strCaption); // button list MSXML2::IXMLDOMNodeListPtr lpButtonList = lpToolbar->GetchildNodes(); MSXML2::IXMLDOMNodePtr lpButton = NULL; for ( ; (lpButton = lpButtonList->nextNode()) != NULL ; ) { MSXML2::IXMLDOMNodePtr lpToolTipNode = lpButton->attributes->getNamedItem("tooltip"); CString strToolTip = (char*)lpToolTipNode->text; MSXML2::IXMLDOMNodePtr lpCommandNode = lpButton->attributes->getNamedItem("command"); CString strCommand = (char*)lpCommandNode->text; strCommand += " "; MSXML2::IXMLDOMNodePtr lpIconNode = lpButton->attributes->getNamedItem("icon"); CString strIcon = (char*)lpIconNode->text; OutputDebugStringX("tooltip=%s, command=%s, icon=%s", strToolTip, strCommand, strIcon); // 添加工具按钮 IAcadToolbarItem *button=NULL; VARIANT index; index.vt = VT_I4; index.lVal = 100l; VARIANT vtFalse; vtFalse.vt = VT_BOOL; vtFalse.boolVal = VARIANT_FALSE; CString strIconFile = lpPath; strIconFile += strIcon; if (!PathFileExists(strIconFile)) { strIconFile = lpPath; strIconFile += "dist.ico"; } if (!PathFileExists(strIconFile)) continue; hr = tlbr->AddToolbarButton(index, CComBSTR(strToolTip), CComBSTR(strToolTip), CComBSTR(strCommand), vtFalse, &button); hr = button->SetBitmaps(CComBSTR(strIconFile), CComBSTR(strIconFile)); button->Release(); } tlbr->Dock(acToolbarDockRight);//acToolbarFloating tlbr->Release(); } tlbrs->Release(); return; }
void CVLDatabase::Open(const string &provider, const string &dataSource, const string &userId, const string &password, IVLDBOpenCallback * callback) { Close(); string connectionString = Format(TEXT("Provider=%s;Data Source=%s;"), (LPCTSTR)provider.c_str(), (LPCTSTR)dataSource.c_str()); try { HRESULT hr = E_FAIL; hr = connection.CreateInstance(__uuidof(Connection)); if (FAILED(hr)) _com_raise_error(hr); hr = recordset.CreateInstance(__uuidof(Recordset)); if (callback) callback->OnOpenState(string("Opening connection")); connection->Open(connectionString.c_str(), userId.c_str(), password.c_str(), adConnectUnspecified); connected = true; if (callback) callback->OnOpenState(string("Retrieving record count")); _RecordsetPtr r = connection->Execute((TEXT("SELECT COUNT(ID) AS RC FROM ") + recordsetName).c_str(), NULL, adCmdText); int rc = r->Fields->Item["RC"]->Value.lVal; if (callback) callback->OnOpenState(string("Allocating memory")); allCapacity = VLDBAllCapacity(rc); allItems = (VLDBItem**)malloc(sizeof(VLDBItem*) * allCapacity); if (callback) callback->OnOpenState(string("Opening recordset in forward only mode")); OpenRecordset(adOpenForwardOnly); if (callback) { callback->OnOpenState(string("Loading data")); callback->OnOpenProgress(false, rc); } DWORD dt = GetTickCount(); while (!recordset->EndOfFile) { if (callback && GetTickCount() - dt > 300) { if (!callback->OnOpenProgress(true, allCount)) break; dt = GetTickCount(); } if (record.idStatus != adFldOK || record.featuresStatus != adFldOK || record.faceIdStatus != adFldOK) throw exception("Can not open database\nError reading data"); AddItem(record.id, record.features, record.featuresLength, record.faceId, record.faceIdLength / sizeof(TCHAR)); recordset->MoveNext(); } if (callback) callback->OnOpenState(string("Opening recordset")); OpenRecordset(); if (callback) callback->OnOpenState(string("Done")); this->provider = provider; this->dataSource = dataSource; } catch (_com_error &e) { if (callback) callback->OnOpenState(string("Error")); try { Close(); } catch (exception &) { // do nothing } ComErrorToException(e, string("Can not open database")); } }
inline HRESULT com_raise_on_failure(HRESULT hr) { if (FAILED(hr)) _com_raise_error(hr); return hr; }