?
Solved

How to create an array of safearrays?

Posted on 1998-04-09
2
Medium Priority
?
1,037 Views
Last Modified: 2013-11-20
I need to pass an array of 1d variants to an automation object.  Right now I am using SafeArrayCreateVector (VT_VARIANT, 0, 2) to create a 1d array and then calling my AddRow(safeArray*) method over and over.

What I want to do is allocate an array of arrays so I can just call my AddRow function once with an array of safearrays.  This may sound confusing so here is a simple example of my data (queried from a database)

Row1:   UserName    ID
Row2:   UserName    ID

Each time I call AddRow (currently), I am passing an array of variants (safearray) that has 2 VARIANTs (one for UserName and one for ID).  I want to be able to pass all the arrays (row 1 through row n) at once.  

Is this possible?  Also, is it possible to create this array dynamically (I am getting this data from a database, so I really don't know how big it is until it's all done reading)?

0
Comment
Question by:tdietz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Expert Comment

by:Thunder041398
ID: 1317595
Maybe you can create an array of pointers to the safearrays...
0
 
LVL 3

Accepted Solution

by:
Norbert earned 200 total points
ID: 1317596
SafeArrayCreateVector can only create 1D arrays.
you need a more dimensional array so you should use SafeArrayCreate
or if you are using VC++ 4.2 or higher the Class COleSafeArray
Using SafeArrayCreate :
build for example a function
SafeArray* CreateMySafeArray(DWORD NumberOfRows)
{
     SafeArray* Psa;
     SAFEARRRAYBOUND* rgsabound=
         new SAFEARRRAYBOUND[NumberOfRows];
     for(DWORD i=0;i<NumberOfRows;i++)
     {
           rgsabound[i].cElements=2;
           rgsabound[i].lbound=0;
     }
     Psa=SafeArrayCreate(VT_VARIANT, NumberOfRows
           , rgsabound);
    delete[] rgsabound;
    return Psa;
}
now the calling function has a pointer to the SafeArray
so the array accessing part of the caller could look like
....
 DWord NumberOfRows=GetRowCount();
 SafeArray* MyArray=CreateMySafeArray( NumberOfRows);
 If(!MyArray)
{
     Error !!!
}
else
{
   // lets fill the array
   Variant *VariantValue;
   for(DWORD i=0;i<NumberOfRows;i++)
   {
           SafeArrayAccessData(MyArray,i,(void**)&VariantValue);
           VariantValue[0].bstrVal=UserName[i].AllocSysString()
          //Assuming UserName is a array of CStrings
          VariantValue[0].vt=VT_BSTR;
          VariantValue[1].lVal=UserID[i];
          VariantValue[1].vt=VT_I4;
          SafeArrayUnaccessData(MyArray);
   }
  //now the array is filled and can delivered to the Automation
  //object as your previous created with SafeArrayCreateVector
}
Using COleSafeArray I have never used for n-Dimensional arrays
so I have no expirience for that
I hope that helps

Regards
   Norbert
PS: Greetings from germany
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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