Solved

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

Posted on 1998-11-08
2
309 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
2 Comments
 

Author Comment

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

Accepted Solution

by:
sudhirbrat earned 200 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

786 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