//方法2 SafeArrayGetElement(SAFEARRAY* psa,long* ailndex,void* pvElem) if(pvDataValues->vt & VT_ARRAY){ switch(pvDataValues->vt){ case VT_BOOL| VT_ARRAY: case VT_UI1 | VT_ARRAY: case VT_I1 | VT_ARRAY: case VT_UI2 | VT_ARRAY: case VT_I2 | VT_ARRAY: case VT_UI4 | VT_ARRAY: case VT_I4 | VT_ARRAY: case VT_R4 | VT_ARRAY: case VT_R8 | VT_ARRAY: case VT_BSTR | VT_ARRAY:{ // Get pointer to array data: SafeArrayLock(pSafeArr);//这个函数调不调,影响未知 CSafeArray *pSafeArr = (CSafeArray *) (pvDataValues->parray); // Process array data if pointer appears to be good: if (pSafeArr){ // Get the array dimensions: DWORD dwCols = pSafeArr->GetNumCols(); //数组的列数 DWORD dwSize = pSafeArr->GetByteLength (); //所有元素占的字节个数 DWORD dwElementNum = pSafeArr->GetNumElements();//共有多少个元素 ULONG cbElements = pSafeArr->cbElements; //每个元素占用的字节个数 LPBYTE lpByte = (LPBYTE)pSafeArr->pvData; //数据的启始位置 DWORD dwCol = 0; for(DWORD idx = 0;idx<dwElementNum;idx++){ if(pvDataValues->vt & VT_UI1){ unsigned char uchValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&uchValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%u"),uchValue); }else if(pvDataValues->vt & VT_I1){ char chValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&chValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%d"),chValue); }else if(pvDataValues->vt & VT_UI2){ unsigned short ushValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&ushValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%u"),ushValue); }else if(pvDataValues->vt & VT_I2){ short shValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&shValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%d"),shValue); }else if(pvDataValues->vt & VT_UI4){ unsigned int uiValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&uiValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%u"),uiValue); }else if(pvDataValues->vt & VT_I4){ int iValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&iValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%d"),iValue); }else if(pvDataValues->vt & VT_R4){ float fValue = 0.0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&fValue); if(FAILED(hres)){ return false; } _stprintf(szNum,_T("%f"),fValue); }else if(pvDataValues->vt & VT_BSTR){ BSTR bstrValue = NULL; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&bstrValue);//bstrValue调用方是否需要释放 if(FAILED(hres)){ return false; } sValues.AddTail(bstrValue); continue; }else if(pvDataValues->vt & VT_I8){ __int64 i64Value = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&i64Value); if (FAILED(hres)){ return false; } TCHAR *pValue = _i64tot(i64Value,szNum,10); }else if(pvDataValues->vt & VT_UI8){ unsigned __int64 ui64Value = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,&ui64Value); if(FAILED(hres)){ return false; } TCHAR *pValue = _ui64tot(ui64Value,szNum,10); }else if(pvDataValues->vt & VT_R8){ double dbValue = 0; HRESULT hres = SafeArrayGetElement(pSafeArr,&idx,dbValue); if(FAILED(hres)){ return false; } _stprintf (szNum, _T("%G"), dbValue); }else{ return false; } } } } break; default:{ return false; } } }
标签:SafeArray,return,hres,pSafeArr,VT,一维,pvDataValues,false,解析 From: https://www.cnblogs.com/dignitys/p/17144743.html