HRESULT GetAssemblyFromAppDomain(_AppDomain* appDomain, const wchar_t* assemblyName, _Assembly **assembly) { SAFEARRAY* safearray; CComSafeArray<IUnknown*> assemblies; CHECKCOM(appDomain->GetAssemblies(&safearray)); assemblies.Attach(safearray); for (int i = 0, n = assemblies.GetCount(); i < n; i++) { CComPtr<_Assembly> a; a = assemblies[i]; if (a == nullptr) continue; CComBSTR assemblyFullName; CHECKCOM(a->get_FullName(&assemblyFullName)); if (assemblyFullName != nullptr && _wcsnicmp(assemblyFullName, assemblyName, wcslen(assemblyName)) == 0) { *assembly = a.Detach(); return S_OK; } } return E_FAIL; }
void lvDCOMInterface::getLabviewValue(const char* param, T* value, size_t nElements, size_t& nIn) { if (value == NULL) { throw std::runtime_error("getLabviewValue failed (NULL)"); } if (param == NULL || *param == '\0') { throw std::runtime_error("getLabviewValue: param is NULL"); } CComVariant v; char vi_name_xpath[MAX_PATH_LEN], control_name_xpath[MAX_PATH_LEN]; _snprintf(vi_name_xpath, sizeof(vi_name_xpath), "/lvinput/section[@name='%s']/vi/@path", m_configSection.c_str()); _snprintf(control_name_xpath, sizeof(control_name_xpath), "/lvinput/section[@name='%s']/vi/param[@name='%s']/read/@target", m_configSection.c_str(), param); CComBSTR vi_name(doPath(vi_name_xpath).c_str()); CComBSTR control_name(doXPATH(control_name_xpath).c_str()); if (vi_name.Length() == 0 || control_name.Length() == 0) { throw std::runtime_error("getLabviewValue: vi or control is NULL"); } getLabviewValue(vi_name, control_name, &v); if ( v.vt != (VT_ARRAY | CVarTypeInfo<T>::VT) ) { throw std::runtime_error("getLabviewValue failed (type mismatch)"); } CComSafeArray<T> sa; sa.Attach(v.parray); nIn = ( sa.GetCount() > nElements ? nElements : sa.GetCount() ); for(LONG i=0; i<nIn; ++i) { value[i] = sa.GetAt(i); } sa.Detach(); }
STDMETHODIMP CotProjTransform::Transform(LONG count, SAFEARRAY** inX, SAFEARRAY** inY, SAFEARRAY** outX, SAFEARRAY** outY) { if(!CheckPointer()) { return E_FAIL; } double *dxs =new double[count]; double *dys =new double[count]; CComSafeArray<double> safeInx; CComSafeArray<double> safeIny; safeInx.Attach(*inX); safeIny.Attach(*inY); for(long i=0;i<count;i++) { dxs[i] =safeInx[i]; dys[i] =safeIny[i]; } safeInx.Detach(); safeIny.Detach(); //转换 (*m_ppTrans)->Transform(count,dxs,dys); //将转换结果放到输出数组中 safeInx.Attach(*outX); safeIny.Attach(*outY); for(long i=0;i<count;i++) { safeInx[i] = dxs[i] ; safeIny[i] = dys[i] ; } safeInx.Detach(); safeIny.Detach(); return S_OK; }
/** * Output = Map of topic id and field data - Map to be deleted by caller */ std::map<long,CComVariant>* RTDClient::readNewData(){ SAFEARRAY *data_sa; long topic_count = 0; HRESULT hr = comObjectScripRTD->RefreshData( &topic_count, &data_sa ); // Pass Address of SAFEARRAY pointer so that we get pointer to 2D safearray if( FAILED(hr) ){ // Output Data has to be deleted by client std::cout << "RefreshData COM failure." << " - hr - " << hr << std::endl; return 0; } CComSafeArray<VARIANT> data; // Passing data_sa as Constructor input will copy it, but we need to destroy it after use data.Attach( data_sa ); // So attach instead and let CComSafeArray handle it ULONG row_count = data.GetCount(1); // No of Rows = 2nd Dimension Count if( row_count == 0) return 0 ; std::map<long,CComVariant> *output = new std::map<long,CComVariant>; // Map: Topicid, Field Data long index[2]; for( ULONG i=0 ; i<row_count; i++ ){ index[0] = 0; // 0,x - Topic ids. 1,x - Data index[1] = i; CComVariant topic_id_var; data.MultiDimGetAt( index, topic_id_var); long topic_id = (long)MiscUtil::getLong( topic_id_var ); index[0] = 1; index[1] = i; CComVariant topic_data_var; data.MultiDimGetAt( index, topic_data_var); if( output->count(topic_id) != 0 && (*output)[topic_id] != topic_data_var ){ std::cout << "Duplicate:"; MiscUtil::printVariant((*output)[topic_id]); std::cout << "-"; MiscUtil::printVariant(topic_data_var); std::cout << std::endl; //abort(); // If exists - we can have multiple topic values in same call => use vector } (*output)[topic_id] = topic_data_var; } return output; }
//---------------------------------------------------------------------------- // HRESULT CAnchoRuntime::fireOnBeforeRequest(const std::wstring &aUrl, const std::wstring &aMethod, const CAnchoRuntime::FrameRecord *aFrameRecord, /*out*/ BeforeRequestInfo &aOutInfo) { CComPtr<ComSimpleJSObject> info; IF_FAILED_RET(SimpleJSObject::createInstance(info)); fillRequestInfo(*info, aUrl, aMethod, aFrameRecord); CComPtr<ComSimpleJSArray> argArray; IF_FAILED_RET(SimpleJSArray::createInstance(argArray)); argArray->push_back(CComVariant(info.p)); CComVariant result; m_pAnchoService->invokeEventObjectInAllExtensions(CComBSTR(L"webRequest.onBeforeRequest"), argArray.p, &result); if (result.vt & VT_ARRAY) { CComSafeArray<VARIANT> arr; arr.Attach(result.parray); //contained data already managed by CComSafeArray VARIANT tmp = {0}; HRESULT hr = result.Detach(&tmp); BEGIN_TRY_BLOCK aOutInfo.cancel = false; for (ULONG i = 0; i < arr.GetCount(); ++i) { Ancho::Utils::JSObjectWrapperConst item = Ancho::Utils::JSValueWrapperConst(arr.GetAt(i)).toObject(); Ancho::Utils::JSValueWrapperConst cancel = item[L"cancel"]; if (!cancel.isNull()) { aOutInfo.cancel = aOutInfo.cancel || cancel.toBool(); } Ancho::Utils::JSValueWrapperConst redirectUrl = item[L"redirectUrl"]; if (!redirectUrl.isNull()) { aOutInfo.redirect = true; aOutInfo.newUrl = redirectUrl.toString(); } } END_TRY_BLOCK_CATCH_TO_HRESULT }
void main(int argc, char**argv) { std::wcout << L"\n ===================================\n"; std::wcout << L"\n TextFinderComponent C++ Interface "; std::wcout << L"\n ===================================\n"; HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (!SUCCEEDED(hr)) { std::wcout << L"\n could not initialize COM"; } try { CComQIPtr<ITextCompCOM> TextFinderComp; TextFinderComp.CoCreateInstance(CLSID_TextCompCOM); if (TextFinderComp != 0) { std::wcout << L"\n =============================================================\n"; std::wcout << "The Text Component Interface was successfully initialized" << std::endl; std::wcout << L"\n =============================================================\n"; InputArgumentParser parser; if (parser.parseCommandLineArgs2TextCompArguments(argc, argv)) { HRESULT h0 = TextFinderComp->InitializeComponent(); HRESULT h1 = TextFinderComp->SetSearchPath(CComBSTR(parser.getDirectoryPath().c_str())); BSTR allPatterns; BSTR recursion; allPatterns = convertstdSTR2BSTR(bool2String(true)) ; //By default find all patterns recursion = convertstdSTR2BSTR(bool2String(false)) ; // By default Recursion is disabled if (parser.getRecursionFlag()) { recursion = convertstdSTR2BSTR(bool2String(true)); } if (!parser.getAllPatternsFlag()) { allPatterns = convertstdSTR2BSTR(bool2String(false)); } HRESULT h2 = TextFinderComp->SetSpecialSearchClause(recursion, allPatterns); HRESULT h3 = TextFinderComp->SetFilePatterns(convertVector2CCOMSafeArray(parser.getFilePatterns()).GetSafeArrayPtr()); HRESULT h4 = TextFinderComp->SetTextPatterns(convertVector2CCOMSafeArray(parser.getTextPatterns()).GetSafeArrayPtr()); if (SUCCEEDED(h0) && SUCCEEDED(h1) && SUCCEEDED(h2) && SUCCEEDED(h3) && SUCCEEDED(h4)) { SAFEARRAY files; SAFEARRAY* pFiles = &files; TextFinderComp->GetQualifyingFileList(&pFiles); CComSafeArray<BSTR> Files; Files.Attach(pFiles); std::wcout << L"\n =============================================================\n"; std::wcout << L"\n =============================================================\n"; std::wcout<<"The Qualifying Files from the Text Search Component via C++ COM interface"<<std::endl; displayCCOMBSTRFiles(Files); std::cout << "End of C++ Client for Text Search Component" << std::endl; std::wcout << L"\n =============================================================\n"; } } else { parser.displayIllegalArgumentMessage(); } } } catch (std::exception& ex) { std::wcout << L"\n Exception Encountered during COM Stuff .. Contact Admin and Bug Him!" << ex.what() << L"\n\n"; return; } std::wcout << L"\n\n"; CoUninitialize(); }
STDMETHODIMP CConsoleObject::BstrPrintf(BSTR* ret, const SAFEARRAY& varg) const { //DUMP_VARIANTSAFEARRAY(&varg); if (ret == NULL) return E_POINTER; CComSafeArray<VARIANT> safearray; HRESULT hr = safearray.Attach(&varg); if (FAILED(hr)) return hr; if (safearray.GetDimensions() != 1) return E_INVALIDARG; CAtlString result(_T("")); _variant_t current; int upper = safearray.GetUpperBound() + 1; for (int i = safearray.GetLowerBound(); i < upper; i++) { current.Attach(safearray[i]); if (current.vt == VT_I4) { result.AppendFormat(_T("%ld"), current.lVal); } else if (current.vt == VT_BSTR) { _bstr_t str(current.bstrVal); int specCount = 0; TCHAR* lastFound = str; TCHAR* found; TCHAR next; int specLength = 0; found = _tcschr(str, _T('%')); if (found == NULL) { result += static_cast<TCHAR*>(str); } else { while (found) { next = *(found + 1); switch (next) { case _T('%') : { specLength = 2; result.CopyChars(result.GetBuffer(), lastFound, static_cast<int>(found + 1 - lastFound)); } case _T('s') : { specLength = 2; _variant_t varstr; GET_NEXT_VARIANT(varstr, VT_BSTR); result.Append(lastFound, static_cast<int>(found - lastFound)); result += varstr.bstrVal; break; } case _T('d') : { specLength = 2; _variant_t varint; GET_NEXT_VARIANT(varint, VT_I4); result.Append(lastFound, static_cast<int>(found - lastFound)); result.AppendFormat(_T("%ld"), varint.lVal); break; } case _T('f') : { specLength = 2; //GET_NEXT_VARIANT(VT_R8); break; } case _T('x') : break; default: break; } found += specLength; lastFound = found; found = _tcschr(found, _T('%')); } // while loop } } else { result += _T("[unsupported argument type]"); } result += _T(' '); current.Detach(); } // for loop safearray.Detach(); result.SetAt(result.GetLength() - 1, _T('\0')); BSTR tmp = result.AllocSysString(); if (tmp == NULL) return E_OUTOFMEMORY; *ret = tmp; return S_OK; }