[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-23
2
Medium Priority
?
1,163 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:edc
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 375 total points
ID: 12656758
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
 
LVL 1

Author Comment

by:edc
ID: 12657346
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question