Solved

How to create an array of safearrays?

Posted on 1998-04-09
2
1,019 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 100 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

707 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