Solved

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

Posted on 1998-11-08
2
307 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
Comment Utility
Edited text of question
0
 
LVL 1

Accepted Solution

by:
sudhirbrat earned 200 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now