tags:

views:

115

answers:

2

Am relatively new to the world of C++. I wish to access the data from a multi-dimensional SAFEARRAY. However when I try to retrieve the value, I get the error 0xC0000005: Access violation reading location 0x40e3e300. Attached below is my code and the marked line is where it errors out. Hopefully someone can shed some light as to how to address 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;
    }
  }
+1  A: 

Your indexing is not right on this line:

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

In C++ two-dimensional arrays are indexed as array[x][y]. Also, indexing starts at 0, so you probably need to fix your erroneous line to something like

_bstr_t theData((BSTR)DataValue[0][i-1],FALSE);
vtorhonen
Thanks for clearing the indexing part, wasn't quite sure. About the indexing starting at 0, I actually wanted my loop to start at position 3, hence the offset. Made those changes but still getting problems reading from the array (same error). Btw the array has the dimension of [2][6]...
jwee
A: 

Guys, managed to resolve it. Nothing fancy 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
    }
  }
jwee