开发者

Getting value from two dimensional Safearray in C++

开发者 https://www.devze.com 2023-01-09 21:32 出处:网络
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 loca

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. H开发者_如何转开发opefully 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;
    }
  }


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
    }
  }


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号