Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to pass an array to a ATL-COM object using VARIANTs?

Posted on 1998-11-08
2
Medium Priority
?
319 Views
Last Modified: 2013-11-25
Environment: WIN NT 4 SP3,   VisualC++ 5.0 SP2

Hi,

I want to create a simple function to pass an array from a MFC-EXE to an ATL-COM object.

The function prototype should look like this:

void SetVariantArray(VARIANT varDestArray,
                     void* pSourceArray,
                     VARTYPE vtType,
                     unsigned long lElements);

where:

VARIANT varDestArray  is the property inside the ATL-COM object (the VARIANT) I want to set.
e.g.: pComObject->vtMyVariant;

void* pSourceArray  is the pointer to the Array which I want to pass to the ATL-COM object
e.g.: pSourceArray = new int[400];

VARTYPE vtType  is the base type of the array (the VARTYPE of each element of the array).
e.g.: vtType = VT_I4

unsigned long lElements  is the number of elements of the Array
e.g.:lElements = 400;

I'm very new to this stuff, and so I'm very insecure about how such a function should look like.

I read, that I would have to use SafeArrays which I have to create and destroy and pass them via the _variant_t wrapper class, but I dont have any glue about how all these things have to be fit together.

Could someone help me please ?

Many thanks in advance.
0
Comment
Question by:Yeti
[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
 

Author Comment

by:Yeti
ID: 1324361
Edited text of question
0
 
LVL 1

Accepted Solution

by:
sudhirbrat earned 600 total points
ID: 1324362
Yes, u should use safe arrays to pass arrays in automation.
U need to create the safe array in MFC app and also free the array. MFC provides API for safearray creation and freeing and accessing the array.

So, Your function SetVariantArray should look like this.

void SetVariantArray(VARIANT varDestArray,
                         void* pSourceArray,
                         VARTYPE vtType,
                         unsigned long lElements);
{
    SAFEARRAYBOUND rgsabound[1];  
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = lElements;
    pSourceArray = (void*)SafeArrayCreate(vtType, 1, rgsabound);
}

This way u can create the array and use.
Refer to Online help for SafeArrayCreate Function, and other functions are also available.

Let me know if this helps u.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

719 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