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
Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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: 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…
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.
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…

839 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