Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 118
Window placement 17 101
Need to impress with my knowledge of .NET 5 104
PL SQL Developer 7 90
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

710 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