Getting value from two-dimensional Safearray in C ++

It is relatively new to the C ++ world. I want to access data from multidimensional SAFEARRAY. However, when I try to get the value, I get the error 0xC0000005: Location of access violation detection 0x40e3e300. The code below is mine, and the marked line is an error. Hope someone can shed some light on how to solve it.

 SAFEARRAY *ArrayCrosstabInfo = GetMainFrame().m_epsComHelper->GetCrosstab(m_arrayFieldnames,start,end);
  COleSafeArray ArrayCrosstab(*ArrayCrosstabInfo,VT_SAFEARRAY);

  BSTR *DataValue;
  ArrayCrosstab.AccessData((void**) &DataValue);

  long lUBoundX;
  long lUBoundY;

  ArrayCrosstab.GetUBound(1,&lUBoundX);
  ArrayCrosstab.GetUBound(2,&lUBoundY);

  long lOffset = 2;
  int nFieldIndex = 0;

  if (lUBoundX > 0 && lUBoundY > 0)
  {
    //only interested in DataValue[0,x]
    for (long i = lOffset; i<=lUBoundY; i++)
    {
      _bstr_t theData((BSTR)DataValue[0,i],FALSE); <==ERRORS HERE
     //Display (BSTR)theData;
    }
  }
+3
source share
2 answers

The guys managed to solve this problem. Nothing special, but here it is.

 SAFEARRAY *ArrayCrosstabInfo = GetMainFrame().m_epsComHelper->GetCrosstab(m_arrayFieldnames,start,end);

  int lOffset = 2;
  long index[2];

  long lUBoundX;
  long lUBoundY;

  SafeArrayGetUBound(ArrayCrosstabInfo, 1, &lUBoundX);
  SafeArrayGetUBound(ArrayCrosstabInfo, 2, &lUBoundY);

  if (lUBoundX >= 0 && lUBoundY >= 0)
  {
    double theResult = 0;
    for (long i=lOffset; i<=lUBoundY; i++)
    {
     index[0] = 0;
     index[1] = i;

     SafeArrayGetElement(ArrayCrosstabInfo, index, &theResult);

     std::ostringstream strs;
     strs << theResult;
     std::string str = strs.str();
     CString cs(str.c_str());
     //display cs
    }
  }
+2
source

Your indexing does not match this line:

_bstr_t theData((BSTR)DataValue[0,i],FALSE);

++ array [x] [y]. , 0, , , :

_bstr_t theData((BSTR)DataValue[0][i-1],FALSE);
+1

Source: https://habr.com/ru/post/1757087/


All Articles