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.
LVL 1
edcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You could use

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

   while ( *(FileNames + i) != NULL) { // assume last entry is NULL

        COleSafeArray osa(*(FileNames + i));
        BSTR* pBstr;

        osa.AccessData(&pBstr);

        //...
        osa.UnaccessData();
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edcAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.