Solved

Creating SAFEARRAY with multiple data types.

Posted on 2000-05-08
4
664 Views
Last Modified: 2011-04-14
I want to create a safe array that contains different types of data. For example, I am trying to create a 2d array with legnth 4. I want all the elements array[0][0], array[0][1], array[0][2], array[0][3], to be BSTRs. I want all the elements array[1][0], array[1][1], array[1][2], array[1][3] to be floats. How can I create this array and set the elements correctly? Please provide sample code.

Here is what I have so far.

  VARIANT ChartData;
  VariantInit( &ChartData );
  ChartData.vt = VT_ARRAY | VT_VARIANT;

  SAFEARRAYBOUND rgb[] = { 4, 0 };
  SAFEARRAY *psa = SafeArrayCreate( VT_VARIANT, 2, rgb );

  if( psa )
  {
    long* data;

    SafeArrayAccessData( psa, ( void ** )&data );

    // SET ARRAY ELEMENTS!!?

    SafeArrayUnaccessData( psa );
  }

  ChartData.parray = psa;
0
Comment
Question by:cdgough
  • 2
4 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2790062
you should build a template class named safearray
something like:

template <class T,int S>
class SafeArray:parent{
   T arr1[S];
public:
SafeArray(){}
T&operator[](int x){
//something like
   return arr1[x];
}
~SafeArray(){}
};

in main to make safearray of BSTRs
and another one for flosts

then to make a container in size two that cont[0] will the first safearray
and cont[1] the second

i hope you got the idea






0
 

Author Comment

by:cdgough
ID: 2790496
This doesn't look right.

I need two different types of variants in the same array. A single array with some BSTRs and some floats.
0
 
LVL 1

Accepted Solution

by:
sharonk earned 100 total points
ID: 2791510
The following C++ code function creates a safearray like the one you requested (with BSTR and floats):

STDMETHODIMP CArrayTest::CreateArray(VARIANT *result)
{
      SAFEARRAY *psa;
      HRESULT hr;
      VARIANT var;            
      SAFEARRAYBOUND rgsabound[2];
      long element[2];
      char sz[256];
      int count;

      rgsabound[0].lLbound = 0;
      rgsabound[0].cElements = 10;
      rgsabound[1].lLbound = 0;
      rgsabound[1].cElements = 10;

      psa = SafeArrayCreate(VT_VARIANT, 2, rgsabound);
      if(psa == NULL)
            return E_FAIL;

      for(count = 0; count < 10; count++)
      {
            // Prepare BSTR value
            sprintf(sz, "Hello%d", count);
            V_BSTR(&var) = SysAllocString(_bstr_t(sz));
            V_VT(&var) = VT_BSTR;
            // Prepare position
            element[0] = 0;
            element[1] = count;
            // Put element
            hr = SafeArrayPutElement(psa, element, &var);                    
            if (!SUCCEEDED(hr))
                  return E_FAIL;
            VariantClear(&var);
                  
            // Prepare float value
            V_R4(&var) = (float)count;
            V_VT(&var) = VT_R4;
            // Prepare position
            element[0] = 1;
            element[1] = count;
            // Put element
            hr = SafeArrayPutElement(psa, element, &var);                    
            if (!SUCCEEDED(hr))
                  return E_FAIL;
            VariantClear(&var);                          
      }
            
      // Copy Results to return Value
      V_VT(result) = VT_ARRAY | VT_VARIANT;
      V_ARRAY(result) = psa;
      
      return S_OK;
}

The following VB code test this function (assuming it resides in an impleemented interface):

    Dim a As New ArrayTest
    x = a.CreateArray
    For i = 0 To 9
        MsgBox x(0, i) & " " & x(1, i)
    Next

0
 

Author Comment

by:cdgough
ID: 2794849
This is exactly what I was looking for. Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
c++ how to tell if the progra is ctl or mfc atl ect 6 98
Excel/Word Add-in in what language? 5 141
passing data from one form to another form in c++ 27 88
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 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