Link to home
Start Free TrialLog in
Avatar of edc
edc

asked on

How to access an array of BSTRs from a SAFEARRAY input parameter

Hello all,
   I have a COM method that makes use of an array of strings.  The IDL prototype is:
  [id(1), helpstring("method Test")] HRESULT Test([in] SAFEARRAY(BSTR) *FileNames);

  The header file has the method prototype: STDMETHOD(Test)(/*[in]*/ SAFEARRAY **FileNames);
  and the cpp file has the method

STDMETHODIMP CTestClass::Test(SAFEARRAY **FileNames)
{
   // Suff happens here
}

What I have not been able to figure out is how to access the strings in the FileNames variable.  Can someone post or point me to code that explains how to iterate through the SAFEARRAY and retrieve the values?

Thanks much.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edc
edc

ASKER

Hi jkr,
   Thanks for the pointer.  It lead me on the right path.  A few syntax changes

STDMETHODIMP CTestClass::Test(SAFEARRAY **FileNames)
{
  // Suff happens here
   int i = 0;
  long numberOfElements=0;
  long lowerBound=0;
  long upperBound=0;

  BSTR* pBstr;
  COleSafeArray osa(*(FileNames), VT_BSTR);
  osa.AccessData((LPVOID*) &pBstr);
  osa.GetLBound(1, &lowerBound);
  osa.GetUBound(1, &upperBound);  
  numberOfElements = upperBound - lowerBound + 1;
  for( int i=0; i<numberOfElements; i++)
  {
     _bstr_t bData = pBstr[i];

  }
  osa.UnaccessData();
 }
 
Thanks for getting me on the right track.