STDMETHODIMP CXRecords::GetRows(VARIANT key, VARIANT* pVal) { if(!m_pFields)return SetErrorInfo(s_errInit); CComSafeArray<VARIANT> bstrArray; VARIANT* pVar; HRESULT hr; int i = 0; int pos; int count = m_listRecords->GetCount(); pos = m_pFields->FindField(key); if(pos < 0 || pos >= (int)m_pFields->GetCount()) return DISP_E_BADINDEX; hr = bstrArray.Create(count); if(FAILED(hr))return hr; pVar = (VARIANT*)bstrArray.m_psa->pvData; for(i = 0; i < count; i ++) { hr = VariantCopy(&pVar[i], (VARIANT*)&m_listRecords->GetValue(i)->m_arrayVariant[pos]); if(FAILED(hr)) { bstrArray.Destroy(); return hr; } } pVal->vt = VT_ARRAY | VT_VARIANT; pVal->parray = bstrArray.Detach(); return S_OK; }
STDMETHODIMP CBDictionary::get_Keys(VARIANT *pKeysArray) { CComSafeArray<VARIANT> bstrArray; VARIANT* pVar; HRESULT hr; m_cs.Enter(); hr = bstrArray.Create((ULONG)m_posArray.GetCount()); if(FAILED(hr)) { m_cs.Leave(); return hr; } pVar = (VARIANT*)bstrArray.m_psa->pvData; for(int i = 0; i < (int)m_posArray.GetCount(); i ++) { hr = VariantCopy(&pVar[i], (VARIANT*)&m_posArray[i]->m_key); if(FAILED(hr)) { m_cs.Leave(); bstrArray.Destroy(); return hr; } } m_cs.Leave(); pKeysArray->vt = VT_ARRAY | VT_VARIANT; pKeysArray->parray = bstrArray.Detach(); return S_OK; }
STDMETHODIMP CDlrComServer::GetIntArray(SAFEARRAY **ppsaRetVal) { CComSafeArray<INT> csaData; void HUGEP *pvData = NULL; HRESULT hr = S_OK; int rgData [] = { 1, 2, 3, 4, 5 }; hr = csaData.Create(5, 0); if (FAILED(hr)) goto Error; hr = SafeArrayAccessData((LPSAFEARRAY)csaData, (void HUGEP **)&pvData); if (FAILED(hr)) goto Error; memcpy((void*)pvData, rgData, sizeof(rgData)); hr = SafeArrayUnaccessData((LPSAFEARRAY)csaData); if (FAILED(hr)) goto Error; *ppsaRetVal = csaData.Detach(); Error: return hr; }
STDMETHODIMP CDlrComServer::GetByteArray(SAFEARRAY **ppsaRetVal) { CComBSTR cbstrTestData(L"GetByteArrayTestData"); CComSafeArray<BYTE> csaData; void HUGEP *pvData = NULL; DWORD cbData; HRESULT hr = S_OK; // create a stream of bytes encoded as UNICODE text cbData = SysStringByteLen(cbstrTestData.m_str); hr = csaData.Create(cbData, 0); if (FAILED(hr)) goto Error; hr = SafeArrayAccessData((LPSAFEARRAY)csaData, (void HUGEP **)&pvData); if (FAILED(hr)) goto Error; memcpy((void*)pvData, cbstrTestData.m_str, cbData); hr = SafeArrayUnaccessData((LPSAFEARRAY)csaData); if (FAILED(hr)) goto Error; if (FAILED(hr)) goto Error; *ppsaRetVal = csaData.Detach(); Error: return hr; }
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; }
STDMETHODIMP CDlrComServer::GetObjArray(SAFEARRAY **ppsaRetVal) { CComSafeArray<LPUNKNOWN> csaData; void HUGEP *pvData = NULL; IUnknown* punkThis = NULL; IUnknown* punkOther = NULL; void* rgData[] = { 0, 0 }; HRESULT hr = S_OK; hr = csaData.Create(2, 0); if (FAILED(hr)) goto Error; hr = SafeArrayAccessData((LPSAFEARRAY)csaData, (void HUGEP **)&pvData); if (FAILED(hr)) goto Error; hr = this->QueryInterface(IID_IUnknown, (void**)&punkThis); if (FAILED(hr)) goto Error; hr = CoCreateInstance(CLSID_DlrComServer, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&punkOther); if (FAILED(hr)) goto Error; rgData[0] = punkThis; rgData[1] = punkOther; memcpy((void*)pvData, rgData, sizeof(rgData)); hr = SafeArrayUnaccessData((LPSAFEARRAY)csaData); if (FAILED(hr)) goto Error; punkThis = 0; punkOther = 0; *ppsaRetVal = csaData.Detach(); Error: if (punkThis) punkThis->Release(); if (punkOther) punkOther->Release(); return hr; }
STDMETHODIMP CLDAPQuery::explodeDn( /* [in] */ BSTR str, /* [in] */ ULONG notypes, /* [retval][out] */ SAFEARRAY **result) { PTCHAR *vals = ldap_explode_dn(CComBSTR(str), notypes); m_errorCode = LdapGetLastError(); if(vals) { CComSafeArray<VARIANT> dnValues; for(LONG i = 0L; vals[i]!=NULL; i++) { dnValues.Add(CComVariant(vals[i])); } ldap_value_free(vals); *result = dnValues.Detach(); } return S_OK; }
STDMETHODIMP CTextSearch::GetResults(SAFEARRAY ** files) { // TODO: Add your implementation code here Search searchService; std::string path = searchService.ConvertBSTRToMBS(_searchPath); //std::string keyword = searchService.ConvertBSTRToMBS(_searchKeyword); std::vector<std::string> keywords; for (int i = 0; i < (int)_searchKeywords.GetCount(); ++i) keywords.push_back(searchService.ConvertBSTRToMBS(_searchKeywords[i].Copy())); std::vector<std::string> results = searchService.StartTextSearch(keywords, path, _searchAll); CComSafeArray<BSTR> temp; for (auto result : results) temp.Add(CComBSTR(result.c_str())); *files = temp.Detach(); return S_OK; }
STDMETHODIMP CBQueue::get_Items(VARIANT *pItemsArray) { CComSafeArray<VARIANT> bstrArray; VARIANT* pVar; HRESULT hr; POSITION pos; int i; m_cs.Enter(); hr = bstrArray.Create((ULONG)m_listItems.GetCount()); if(FAILED(hr)) { m_cs.Leave(); return hr; } pVar = (VARIANT*)bstrArray.m_psa->pvData; pos = m_listItems.GetHeadPosition(); i = 0; while(pos) { hr = VariantCopy(&pVar[i ++], &m_listItems.GetNext(pos)); if(FAILED(hr)) { m_cs.Leave(); return hr; } } m_cs.Leave(); pItemsArray->vt = VT_ARRAY | VT_VARIANT; pItemsArray->parray = bstrArray.Detach(); return S_OK; }
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; }